Ben S.
Ben S.

Reputation: 3545

ggplot plotting problems and error bars

So I have some data that I imported into R using read.csv.

d = read.csv("Flux_test_results_for_R.csv", header=TRUE)
rows_to_plot = c(1,2,3,4,5,6,13,14)
d[rows_to_plot,]

It looks like it worked fine:

> d[rows_to_plot,]
        strain selective        rate      ci.low     ci.high
1         4051       rif 1.97539e-09 6.93021e-10 5.63066e-09
2         4052       rif 2.33927e-09 9.92957e-10 5.51099e-09
3  4081 (mutS)       rif 1.32915e-07 1.05363e-07 1.67671e-07
4  4086 (mutS)       rif 1.80342e-07 1.49870e-07 2.17011e-07
5  4124 (mutL)       rif 5.53369e-08 4.03940e-08 7.58077e-08
6  4125 (mutL)       rif 1.42575e-07 1.14957e-07 1.76828e-07
13    4760-all       rif 6.74928e-08 5.41247e-08 8.41627e-08
14    4761-all       rif 2.49119e-08 1.91979e-08 3.23265e-08

So now I'm trying to plot the column "rate", with "strain" as labels, and ci.low and ci.high as boundaries for confidence intervals.

Using ggplot, I can't even get the plot to work. This gives a plot where all the dots are at 1 on the y-axis:

g <- ggplot(data=d[rows_to_plot,], aes(x=strain, y=rate))
g + geom_dotplot() 

Attempt at error bars:

error_limits = aes(ymax = d2$ci.high, ymin = d2$ci.low)
g + geom_errorbar(error_limits)

As you can tell I'm a complete noob to plotting things in R, any help appreciated.

Answer update There were two things going on. As per boshek's answer, which I selected, I it seems that geom_point(), not geom_dotplot(), was the way to go.

The other issue was that originally, I filtered the data to only plot some rows, but I didn't also filter the error limits by row. So I switched to:

d2 = d[c(1,2,3,4,5,6,13,14),]
error_limits = aes(ymax = d2$ci.high, ymin = d2$ci.low)
g = ggplot(data=d2, ...etc...

Upvotes: 0

Views: 414

Answers (1)

boshek
boshek

Reputation: 4406

A couple general comments. Get away from using attach. Though it has its uses, for beginners it can be very confusing. Get used to things like d$strain and d$selective. That said, once you call the dataframe with ggplot() you can refer to variables in that dataframe subsequently just by their names. Also you really need to ask questions with a reproducible example. This is a very important step in figuring out how to ask questions in R.

Now for the plot. I think this should work:

error_limits = aes(ymax = rate + ci.high, ymin = rate - ci.low)

ggplot(data=d[rows_to_plot,], aes(x=strain, y=rate)) +
 geom_point() +
 geom_errorbar(error_limits)

But of course this is untested because you haven't provided a reproducible examples.

Upvotes: 1

Related Questions