\n
Created on 2021-08-31 by the reprex package (v2.0.0)
\ndata
\nMediaPercentage2 <- data.frame(\n group = c("Messengers", "Social Media", "Other Media"),\n mediaTime = c(90, 35, 25)\n)\n
\n","author":{"@type":"Person","name":"Peter"},"upvoteCount":2}}}Reputation: 257
I might have misunderstood this, but from what I read usually a pie chart on ggplot shows a legend by default. However there is none in my plot and I can't find how to put it there. I tried explicitly putting show.legend = TRUE but that didn't change anything.
I'd like to replace the geom_label() with a legend.
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"))
What causes the missing legend and how can I make it visible?
Upvotes: 0
Views: 345
Reputation: 12739
Place fill = group
in the aes
argument and use scale_fill_manual
to control the mapping between fill colour and the grouping value.
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")
Created on 2021-08-31 by the reprex package (v2.0.0)
data
MediaPercentage2 <- data.frame(
group = c("Messengers", "Social Media", "Other Media"),
mediaTime = c(90, 35, 25)
)
Upvotes: 2