【发布时间】:2020-05-11 03:07:02
【问题描述】:
我正在尝试创建此处提到的圆形堆叠条形图 (https://www.r-graph-gallery.com/299-circular-stacked-barplot.html)。当我进入制作情节的步骤时出现以下错误(下面以粗体显示):
错误:美学长度必须为 1 或与数据相同 (26):hjust
运行rlang::last_error() 以查看发生错误的位置。
另外:警告信息:
删除了包含缺失值的 208 行 (position_stack)。
这就是我的数据的样子(5 列 70 行):
个人;团体;值1;值2;值3;价值4
这是我的代码:
以整齐的格式(长格式)转换数据
data <- data %>% gather(key = "observation", value="value", -c(1,2))
制作情节
p <- ggplot(data) +
geom_bar(aes(x=as.factor(id), y=value, fill=observation), stat="identity", alpha=0.5) +
scale_fill_viridis(discrete=TRUE) +
geom_segment(data=grid_data, aes(x = end, y = 0, xend = start, yend = 0), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 2, xend = start, yend = 2), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 4, xend = start, yend = 4), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 6, xend = start, yend = 6), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 8, xend = start, yend = 8), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
ggplot2::annotate("text", x = rep(max(data$id),5), y = c(0, 2, 4, 6, 8), label = c("0", "2", "4", "6", "8") , color="grey", size=6 , angle=0, fontface="bold", hjust=1) +
ylim(-150,max(label_data$tot, na.rm=T)) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar() +
geom_text(data=label_data, aes(x=id, y=tot+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=5, angle= label_data$angle, inherit.aes = FALSE ) +
geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE ) +
geom_text(data=base_data, aes(x = title, y = -18, label=group), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)
**ggsave(p, file="output1.png", width=10, height=10)**
我将不胜感激。
谢谢!!
个人群体价值1价值2价值3价值4
Biomarker1 Group1 0 1 2 2
Biomarker2 Group2 0 1 0 2
Biomarker3 Group2 0 1 0 1
Biomarker4 Group3 1 2 1 0
Biomarker5 Group4 0 2 4 1
Biomarker6 Group4 0 1 0 1
Biomarker7 Group4 0 1 0 1
Biomarker8 Group5 0 1 0 1
Biomarker9 Group6 0 1 1 1
Biomarker10 Group6 0 2 1 1
【问题讨论】:
-
您是否使用了该帖子中的相同模拟数据?
-
您好!如果你们(a)缩小问题范围并且(b)使您的示例可重现,我们可以为您提供更好的帮助。对于 (a),注释掉情节中的所有
geoms。然后一次通过 1 取消注释它们并运行绘图代码,直到找到错误。由于您的错误提到“位置堆栈”,它可能是geom_bar行......然后,对于(b),您为我们没有的数据共享了大约 20 行数据操作代码。在绘制数据之前你对数据做了什么并不重要——重要的是你试图绘制的数据...... -
因此,当您确定是哪一行导致错误时,请与我们分享这些数据。共享数据的最佳方式是使用
dput(),因为它是可复制/可粘贴的。如果问题是您的geom_bar行,它使用data(不是base_data或label_data或grid_data),请在控制台中输入dput(data[1:20, ])并将结果编辑到您的问题中。这将是我们测试解决方案的前 20 行。 (当然,如果前 20 行不能说明问题,请使用不同的子集。) -
而且,当您编辑时,您可以通过删除所有数据争吵等来缩短问题。这将使您的问题更加平易近人、更加集中,并且您将更快地获得帮助。
-
你的代码在 R4.0.0 和 ggplot2 3.3.0 上为我工作
标签: r bar-chart polar-coordinates