为什么我的 ggplot 饼图不显示图例?
Why does my ggplot pie chart not show a legend?
我可能误解了这一点,但从我阅读的内容来看,ggplot 上的饼图通常默认显示图例。但是我的情节中有 none,我找不到如何把它放在那里。我尝试明确地输入 show.legend = TRUE 但这并没有改变任何东西。
我想用图例替换 geom_label()。
library (ggplot2)
MediaPercentage2 <- data.frame(
group = c("Messengers", "Social Media", "Other Media"),
mediaTime = c(90, 35, 25)
)
print(
ggplot(
MediaPercentage2, aes(x = "", y = mediaTime), fill = group) +
geom_bar(stat= "identity", color = "black", fill = c("blue", "white", "orange"), show.legend= TRUE) +
coord_polar(theta = "y")+
geom_label(aes(label = group)
,position = position_stack(vjust = 0.5))+
ggtitle ("Title"))
图例缺失的原因是什么?我怎样才能让它可见?
将fill = group
放在aes
参数中,并使用scale_fill_manual
控制填充颜色和分组值之间的映射。
library(ggplot2)
ggplot(MediaPercentage2, aes(x = "", y = mediaTime, fill = group)) +
geom_col(color = "black") +
coord_polar(theta = "y")+
geom_label(aes(label = group),
position = position_stack(vjust = 0.5),
show.legend = FALSE)+
scale_fill_manual(breaks = c("Messengers", "Social Media", "Other Media"),
values = c("blue", "orange", "white"))+
ggtitle ("Title")
由 reprex package (v2.0.0)
于 2021-08-31 创建
数据
MediaPercentage2 <- data.frame(
group = c("Messengers", "Social Media", "Other Media"),
mediaTime = c(90, 35, 25)
)
我可能误解了这一点,但从我阅读的内容来看,ggplot 上的饼图通常默认显示图例。但是我的情节中有 none,我找不到如何把它放在那里。我尝试明确地输入 show.legend = TRUE 但这并没有改变任何东西。
我想用图例替换 geom_label()。
library (ggplot2)
MediaPercentage2 <- data.frame(
group = c("Messengers", "Social Media", "Other Media"),
mediaTime = c(90, 35, 25)
)
print(
ggplot(
MediaPercentage2, aes(x = "", y = mediaTime), fill = group) +
geom_bar(stat= "identity", color = "black", fill = c("blue", "white", "orange"), show.legend= TRUE) +
coord_polar(theta = "y")+
geom_label(aes(label = group)
,position = position_stack(vjust = 0.5))+
ggtitle ("Title"))
图例缺失的原因是什么?我怎样才能让它可见?
将fill = group
放在aes
参数中,并使用scale_fill_manual
控制填充颜色和分组值之间的映射。
library(ggplot2)
ggplot(MediaPercentage2, aes(x = "", y = mediaTime, fill = group)) +
geom_col(color = "black") +
coord_polar(theta = "y")+
geom_label(aes(label = group),
position = position_stack(vjust = 0.5),
show.legend = FALSE)+
scale_fill_manual(breaks = c("Messengers", "Social Media", "Other Media"),
values = c("blue", "orange", "white"))+
ggtitle ("Title")
由 reprex package (v2.0.0)
于 2021-08-31 创建数据
MediaPercentage2 <- data.frame(
group = c("Messengers", "Social Media", "Other Media"),
mediaTime = c(90, 35, 25)
)