Hello,
I have a violin plot where, for each x, there are different figures, coloured according to another variable and positioned using position="dodge".
This can be explained better with some code:
# reduce the size of the diamonds dataset, for faster debugging
diamonds.short = subset(diamonds, cut == "Premium" & x > 7)
# plot violin plots
ggplot(data=diamonds.short, aes(x=cut, y=price)) + geom_violin(aes(color=color))
On this plot, I want to add some text labels, showing the price of all the diamonds that have a value of "x" higher than 9.
This is how it should look like:
-
http://bioevo.upf.edu/~gdallolio/images/ggplot_question_wanted.png
However, this is what I have obtained so far:
-
http://bioevo.upf.edu/~gdallolio/images/ggplot_question_obtained.png
Complete code:
library(ggplot2)
diamonds.short = subset(diamonds, cut == "Premium" & x > 7)
ggplot(data=diamonds.short, aes(x=cut, y=price)) + geom_violin(aes(color=color)) + geom_text(data=subset(diamonds.short, x>9), aes(label=carat))
Each of the text labels should be showed inside the violin plot where they belong. For example, if the diamond that has (price = 2.71) belongs to "E" color, its label should be shown inside the E figure.
I suspect that the solution lays in giving a correct value of "position" to geom_text, but so far I could not find the correct way.
This is what I have tried so far, but none of these work:
library(ggplot2)
diamonds.short = subset(diamonds, cut == "Premium" & x > 7)
ggplot(data=diamonds.short, aes(x=cut, y=price)) + geom_violin(aes(color=color)) + geom_text(data=subset(diamonds.short, x>9), aes(label=carat, color=color), position="dodge")
ggplot(data=diamonds.short, aes(x=cut, y=price)) +
geom_violin(aes(color=color)) + geom_text(data=subset(diamonds.short,
x>9), aes(label=carat, color=color, position=color))
ggplot(data=diamonds.short, aes(x=cut, y=price)) +
geom_violin(aes(color=color)) + geom_text(data=subset(diamonds.short,
x>9), aes(label=carat, color=color), position=position_dodge(width=0.9))
ggplot(data=diamonds.short, aes(x=cut, y=price)) +
geom_violin(aes(color=color)) + geom_text(data=subset(diamonds.short,
x>9), aes(label=carat, color=color), position=position_identity(width=0.9))
The best I was able to do so is to paste cut and color into a new column, and use that for scale_x:
diamonds.short$cut_color = paste(diamonds.short$cut, diamonds.short$color, sep='_')
ggplot(data=diamonds.short, aes(x=cut_color, y=price)) + geom_violin(aes(color=color)) + geom_text(data=subset(diamonds.short, x>9), aes(label=carat, color=color))
The problem is that now I don't know how to remove the extra labels on the X axis. Plus, I think that there is should be some better way to do this, using position.
What is the best ggplot2-ish way to do this? How can I plot the labels on the correct violin?
Thanks,
Gio
--
Giovanni Dall'Olio, phd student
IBE, Institut de Biologia Evolutiva, CEXS-UPF (Barcelona, Spain)
My blog on bioinformatics:
http://bioinfoblog.it