R ggplot2饼图,数字不';不出现
我对数据可视化和ggplot2的R非常陌生。我试图在饼图中可视化一些数据。我使用的代码如下:R ggplot2饼图,数字不';不出现,r,ggplot2,pie-chart,R,Ggplot2,Pie Chart,我对数据可视化和ggplot2的R非常陌生。我试图在饼图中可视化一些数据。我使用的代码如下: percentageData <- data.frame(Year = "1987", TypeOfDelays = c(percDepDelays[1], percArrDelays[1], percAntDepdelays[1], percAntArrDelays[1]), L
percentageData <- data.frame(Year = "1987",
TypeOfDelays = c(percDepDelays[1], percArrDelays[1], percAntDepdelays[1], percAntArrDelays[1]),
Label = factor(c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")))
labels = c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")
ggplot(data = percentageData) +
geom_bar(aes(x="", y=TypeOfDelays, fill=Label), stat = "identity", width = 1) +
coord_polar(theta = "y", start = 0) +
theme_void() +
geom_text(aes(x = 1, y=cumsum(TypeOfDelays) - TypeOfDelays/2, label=labels))
percentageData为了得到您想要的,您可以在以下几点上调整代码:
- 将
aes
移动到ggplot()部分
- 从
geom_文本中的aes
中删除x
和y
规范
- 将
position=position\u堆栈(vjust=0.5)
添加到geom\u文本中
最终代码:
ggplot(data = percentageData, aes(x="", y = TypeOfDelays, fill = Label)) +
geom_bar(stat = "identity", width = 1) +
geom_text(aes(label = labels), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y", start = 0) +
theme_void()
其中:
这些数字没有出现,因为您没有要求ggplot2
包含它们。一种可能性:
ggplot(data = percentageData, aes(x="", y = TypeOfDelays, fill = Label)) +
geom_bar(stat = "identity", width = 1) +
geom_text(aes(label = paste0(labels, ': ', TypeOfDelays,' %')), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y", start = 0) +
theme_void()
其中:
使用数据:
percentageData <- data.frame(Year = "1987",
TypeOfDelays = c(30, 45, 5, 20),
Label = factor(c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")))
labels <- c("Departure delays", "Arrival Delays", "Early Departure", "Early arrival")
percentageData我正试图找出你的数据:1)你链接的数据集中没有百分比。你是如何计算它们的?2) 什么是percAntDepdelays
和percantardelays
?3) 在第一个例子中,它不是用大写字母D
表示延迟吗?请用这些疑问的答案编辑问题,不要在评论中回答。