ggplot2 中一个图上的条形图和堆积条形图
Bar and stacked bars on one plot in ggplot2
晚上好,谁能帮忙出谋划策。我有一些这样的数据:
DF <- data.frame(
country_dest=c("Russia", "Germany", "Ukraine" ,"Kazakhstan",
"United States", "Italy", "Israel", "Belarus"),
SumTotal=c(7076562,2509617,1032325,680137,540630,359030,229186,217623)
)
用单独的 8 个柱来绘制它没什么大不了的,但我想知道是否可以用 3 个柱来绘制它,其中第一个柱将包含俄罗斯的数据(例如)第二个将是德国、乌克兰、哈萨克斯坦、美国和意大利的堆叠条,也许有一些传说可以了解谁是谁,第三个是白俄罗斯和以色列的堆叠条。
在网上找到了创建0值新DF的解决方案,但不是很明白。
比你提前!
那么,您需要将分组信息添加到您的数据中。然后它变得简单明了。这是一个策略
#define groups
grp <-c("Russia"=1, "Germany"=2, "Ukraine"=2,"Kazakhstan"=2,
"United States"=2, "Italy"=2, "Israel"=3, "Belarus"=3)
DF$grp<-grp[as.character(DF$country_dest)]
#ensure plotting order
DF$country_dest <- factor(DF$country_dest, levels=DF$country_dest)
#draw plot
ggplot(DF, aes(x=grp, y=SumTotal, fill=country_dest)) +
geom_bar(stat="identity")
这会给你
您可能希望为您的群组添加更具描述性的标签。
晚上好,谁能帮忙出谋划策。我有一些这样的数据:
DF <- data.frame(
country_dest=c("Russia", "Germany", "Ukraine" ,"Kazakhstan",
"United States", "Italy", "Israel", "Belarus"),
SumTotal=c(7076562,2509617,1032325,680137,540630,359030,229186,217623)
)
用单独的 8 个柱来绘制它没什么大不了的,但我想知道是否可以用 3 个柱来绘制它,其中第一个柱将包含俄罗斯的数据(例如)第二个将是德国、乌克兰、哈萨克斯坦、美国和意大利的堆叠条,也许有一些传说可以了解谁是谁,第三个是白俄罗斯和以色列的堆叠条。
在网上找到了创建0值新DF的解决方案,但不是很明白。
比你提前!
那么,您需要将分组信息添加到您的数据中。然后它变得简单明了。这是一个策略
#define groups
grp <-c("Russia"=1, "Germany"=2, "Ukraine"=2,"Kazakhstan"=2,
"United States"=2, "Italy"=2, "Israel"=3, "Belarus"=3)
DF$grp<-grp[as.character(DF$country_dest)]
#ensure plotting order
DF$country_dest <- factor(DF$country_dest, levels=DF$country_dest)
#draw plot
ggplot(DF, aes(x=grp, y=SumTotal, fill=country_dest)) +
geom_bar(stat="identity")
这会给你
您可能希望为您的群组添加更具描述性的标签。