ggplot2 - R: Bar plot on a continous x-axis (time-scaled) -


i'm new r please comment on see.

i have data taken @ different timepoints, under 2 conditions (for 1 timpoint) , want plot bar plot errorbars , bars @ appropriate timepoint.

i have (stolen question on site):

library(ggplot2) example <- data.frame(tp = factor(c(0, "14a", "14b", 24, 48, 72)), means  = c(1, 2.1, 1.9, 1.8, 1.7, 1.2), std = c(0.3, 0.4, 0.2, 0.6, 0.2, 0.3)) ggplot(example, aes(x = tp, y = means)) +     geom_bar(position = position_dodge()) +    geom_errorbar(aes(ymin=means-std, ymax=means+std)) 

now timepoints factor, fact there unequal distribution of measurements across time makes plot less nice.!

this how imagine graph :

enter image description here

i find ggplot2 package can give nice graphs, have lot more difficulty understanding have other r stuff.

before r, have realize in bar plot x axis needs numeric value. if treat them factors software assumes equal spacing between bars default. x-values each of bars in case? can (0, 14, 14, 24, 48, 72) plot 2 bars @ point 14 don't seem want. have come x-values.

joran provides elegant solution modifying width of bars @ position 14. modifying code given joran make bars fall @ right position in x-axis, final solution is:

library(ggplot2) example <- data.frame(tp = factor(c(0, "14a", "14b", 24, 48, 72)), means  = c(1, 2.1, 1.9, 1.8, 1.7, 1.2), std = c(0.3, 0.4, 0.2, 0.6, 0.2, 0.3))  example$tp1 <- gsub("a|b","",example$tp) example$grp <- c('a','a','b','a','a','a') example$tp2 <- as.numeric(example$tp1)  ggplot(example, aes(x = tp2, y = means,fill = grp)) +     geom_bar(position = "dodge",stat = "identity") +    geom_errorbar(aes(ymin=means-std, ymax=means+std),position = "dodge") 

enter image description here


Comments