Doubts running ggplot 2: out-of-order data, and error bar

Asked

Viewed 81 times

2

Hello. I’m trying to use ggplot2 to make a graph on R. This graph is a bar graph that deals with various concentrations (each concentration has a bar) on the X axis, which is associated with a dependent variable on the Y axis which is parasitic growth inhibition (%).

However, I found a problem: It puts the concentrations out of order (The right one would be 13.07, then 6.53, then 3.26 etc).

inserir a descrição da imagem aqui

The code I used

library(ggplot2)

comp_1_concentrations = c('13.07','6.53','3.26','1.63','0.81','0.4','0.2')

comp_1_inhibition = c(78,50,28,15,10,2,1.5)

comp_1_desv_pad = c(10,1,10,1.5,10,1,0.2)

data <- data.frame(
  name=comp_1_conc,
  value=comp_1_inhib,
  sd=comp_1_desv
)

ggplot(data) +
  geom_bar( aes(x=name, y=value), stat="identity", fill="black", alpha=1.7,color='black') + 
  geom_errorbar( aes(x=name, ymin=value, ymax=value+sd), width=0.5, colour="black", alpha=0.9, size=0.8)+theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
                                                                                                                            panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+labs(x = 'Concentrations (µM)')+labs(y = 'Growth inhibition (%)')+labs(title = 'Compound 1')

1 answer

3

Let’s split the problems.

1) Change the order of the X-axis:

First of all, a small change of the data that has the variable name as a factor (that introduces some problems that will not help us now). stringsAsFactors = FALSE, keeps this variable as text. I imagine you want it like this.

data <- data.frame(
  name=comp_1_concentrations,
  value=comp_1_inhibition,
  sd=comp_1_desv_pad,
  stringsAsFactors = FALSE
)

With this new data, let’s go to the graph. The function reorder allows reordering the values of the first argument based on the values of the second argument. Turning them into numbers I believe they take the correct order.

p1 <- ggplot(data) +
  geom_bar(aes(x = reorder(name, as.numeric(name)), y = value), 
           stat = "identity", 
           fill = "black", 
           alpha = 0.7)
p1

inserir a descrição da imagem aqui

2) Remove the background grid

To do this we use the function theme and their arguments. To better understand how they work, I recommend your documentation.

The argument panel.grid (with their respective divisions in major and minor and x and y) controls the background grids. To eliminate them we must assign element_blank() for him.

p2 <- p1 +
  theme(panel.grid = element_blank())
p2

inserir a descrição da imagem aqui

3) Error bars and bars

In order for the error bars to show only the "positive" part of the error, simply use the value, and do not valor - sd as ymin. So we have:

p2 + 
  geom_errorbar(aes(x = name, ymin=value, ymax=value+sd), 
                width=0.5, colour="black", 
                alpha=0.9, size=1.3)

fig3

  • Thank you. Is there still a way to let the order decrease? I tried to use the decreasing argument = TRUE but there is no such function.

  • You can i) invert the vector with the function rev() or ii) reorder by -vetor (reorder(x, -y)) - a little hack

  • Where do I put this command? Where do I put it specifically? If I put it in the definition of vectors (first lines) it doesn’t change

  • In the first geometry. p + geom_bar(aes(x = reorder(name, -as.numeric(name)), y = value), ...)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.