1

that's my first question in here, please, don't be angry if anything..

the point is:

I have several timepoints (graft, day 0, day 60 etc...) and several samples for each. The samples have been measured and so I have several numbers for each timepoint. I'd like to make a dot plot placing dots according to the measured values (on Y-axis) grouped by timepoints (on X-axis). I'm totally new with ggplot, but I've succesfully created the plot. The examplary data.frame and code for the plot looks like:

data=data.frame(value=1:30, time=rep(c('d0', 'd1', 'd2'), each=10))

str(data)
'data.frame':   30 obs. of  2 variables:
$ value: num  1 2 3 4 5 6 7 8 9 10 ...
$ time : Factor w/ 3 levels "d0","d1","d2": 1 1 1 1 1 1 1 1 1 1 ...

g=ggplot(data=data, aes(x=time, y=value))
g+geom_point()

But I have no idea how to add short lines to mark median values for each timepoint... Guess I should use geom_segments somehow, but I don't know how to deal with x, xend, y and yend in that case... Could anyone advise me smth?

Thank's to all!

ivan-z
  • 13
  • 1
  • 5
  • 3
    Please see [how to create a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for better ways to share sample data. Since we can't run the code in it's current state, it's difficult to see exactly what your plot might look like. Remove all unnecessary elements to make the code as minimal as possible. (are the themes essential to the question?) – MrFlick Jul 01 '15 at 19:21
  • Thank you for comment. I've edited the question, hope it's much better now... – ivan-z Jul 01 '15 at 21:58
  • a box plot is a usefull way of displaying summary stats: `ggplot(data=data, aes(x=time, y=value)) + geom_boxplot() + geom_point()` – tospig Jul 03 '15 at 02:41
  • @topsig Thank you. I thought about geom_boxplot, but failed to delete borders of the boxes. – ivan-z Jul 03 '15 at 09:58

1 Answers1

7

This is the sort of thing you can use stat_summary for with the geom of your choice. For example, you could add the median as a point using a long dash as the symbol and changing the size to something that looks OK.

ggplot(data = data, aes(x = time, y = value)) + 
    geom_point() +
    stat_summary(fun.y = "median", geom = "point", pch = "_", size = 25)

enter image description here

Another alternative would to be use a crossbar or errorbar as the geom, but you have to set the ymin and ymax for these. You can set those aesthetics to ..y.. in order to simply set them to be the same as the y variable. Changing the width changes how long the lines are.

ggplot(data = data, aes(x = time, y = value)) + 
    geom_point() +
    stat_summary(fun.y = "median", geom = "crossbar", 
               mapping = aes(ymin = ..y.., ymax = ..y..), width = .25)

enter image description here

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Thank you so much for your very useful comment! It also improved my understanding of ggplot. My solution for the task was using of geom_segments with new data.frame containing only medians. But it's just kind of "stub patch" cause of my poor knowledge. As for your solution, seems the second variant a bit more flexible cause of the possibility to play with thickness of lines for median values. – ivan-z Jul 03 '15 at 09:42