在这样的情节
library(ggplot2)
df <- data.frame(class = c("a","b","a","b"), date = c(2009,2009,2010,2010), volume=c(1,1,2,0))
df <- df %>% group_by(date) %>% mutate(volumep = 100 * volume/sum(volume))
ggplot(df, aes(x = date, y = volumep, fill = class, label = volumep)) +
geom_bar(stat = "identity") +
geom_text(size = 3, position = position_stack(vjust = 0.5)) + coord_flip()
如何增加右侧(类)框中的文本以及如何使 x 轴具有 0、25、50 和 100 值?
最佳答案
要回答这个问题,只需调整所涉及的美学、y
和 size
即可。
ggplot(df, aes(x = date, y = 100*volume, fill = class, label = volume)) +
geom_bar(stat = "identity") +
geom_text(size = c(3, 3, 5, 5), position = position_stack(vjust = 0.5)) +
coord_flip() +
ylab("volume")
另一种选择是首先改变
volume
的值。这样的话就不需要手动设置y轴标签了。
问题编辑后,代码如下。
library(ggplot2)
library(dplyr)
df %>%
group_by(date) %>%
mutate(volume = 100*volume/sum(volume)) %>%
ggplot(aes(x = date, y = volume, fill = class, label = volume)) +
geom_bar(stat = "identity") +
geom_text(size = c(3, 3, 5, 5), position = position_stack(vjust = 0.5)) +
coord_flip()
关于r - X 轴和堆叠条中的右框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60170270/