r - 堆叠条中的 X 轴和右框
问题描述
在这样的情节中
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")
另一个选项是firstmutate
的值。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()
推荐阅读
- python - 获取 AttributeError 试图在 tkinter 应用程序中访问 math.sqrt(x)
- javascript - mat-dialog 在下次打开时记住以前的数据
- python - 如何在 Cassandra 中正确配置和执行 BatchStatement?
- java - 春季微服务期间的数据库问题
- flutter - 无法使用对齐小部件和对齐属性对齐行的子级
- networking - 无法在应用层检测碰撞?唔
- python - 多处理价值的解释?(意外的输出)
- swift - 不能执行任何到具体类型的泛型类型
- pdf - 在 itext7 中添加到 PDF 时旋转的图像
- file - 将固定块大小文件系统中的 1K 块大小更改为 2K 将如何影响磁盘吞吐量/利用率