I would like to place *
above some bars in my bar plot. This would be straightforward if I was not using the fill
aesthetic, but with it, I cannot figure out how to place stars over the bars, rather than the x-axis ticks.
I have tried to use geom_text
, and was unsuccessful. I see that I can add them manually using annotate
, but this is an inelegant solution.
Here is a minimal example of the dataset and plot I would like to add *
to:
df <- data.frame(age = c('year1', 'year1', 'year2', 'year2'),
category = c('cat1', 'cat2', 'cat1', 'cat2'),
value = 1:4,
star = c("", "*", "", "")
)
g <- ggplot(data = df, aes(category, proportion, fill = age))
g <- g + geom_bar(stat = "identity", position = position_dodge())
g
How can I add the *
from my data set in the star
column over the corresponding bar?
I would suggest this approach using geom_text()
in dodge style:
library(ggplot2)
#Data
df <- data.frame(age = c('year1', 'year1', 'year2', 'year2'),
category = c('cat1', 'cat2', 'cat1', 'cat2'),
value = 1:4,
star = c("", "*", "", "")
)
#Plot
g <- ggplot(data = df, aes(category, value, fill = age,label=star))
g <- g + geom_bar(stat = "identity", position = position_dodge())
g <- g + geom_text(position = position_dodge(0.9),vjust=-0.5)
g
Output:
Let me know if that is what you wanted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.