首页 > 解决方案 > 具有 1200 个值的 ggplot2 堆叠图

问题描述

嗨ggplot2战士!

我正在努力处理包含大约 1200 个堆叠值的堆叠图。我有一个 df 有 4 个变量

'data.frame':   4935 obs. of  4 variables:
 $ ISO3   : Factor w/ 133 levels "AGO","ALB","ARE",..: 23 105...
 $ band   : int  1 1 1 2 1 1 1 2 1 1 ...
 $ upbound: num  1000 1000 1000 2000 1000 1000 1000 2000 1000 1000 ...
 $ ET1    : num  3981 1280 1223 1096 772 ...

我需要绘制国家(ISO3)与 ET1,按波段堆叠。

代码:

library(dplyr); library(ggplot2); library(scales); library(ggsci); library(gridExtra); library(RColorBrewer); library(tidyr); library(reshape2)

#df
ex1 <- read.csv("example.csv")
ET <- select(ex, ET1) # used later 
ex <-  ex1  %>%  # to get descent values and graph according 
  arrange(desc(ET1, na.rm = TRUE))

#ex graph 
ggplot(data = ex) + 
  geom_bar(mapping = aes(x = ISO3, fill = as.factor(upbound))) + #use as.factor to stack (correct?)
  theme(legend.position="none", text=element_text(size=25)) + # none because there are 1200 values in legend
  xlab("Country") + ylab("ET1") +
  coord_flip() + #tested up to here # save 1500x4000
  #scale_fill_continuous(aes(as.numeric(upbound)),breaks = c(500, 1000)) + #doesn't work
  #scale_x_log10(minor_breaks = log10(ET)) +#doesn't work

这里是图表前任

#ex_a graph
ggplot(data = ex) + 
  geom_bar(mapping = aes(x = ISO3, fill = as.factor(upbound))) +
  theme(legend.position="bottom", text=element_text(size=25)) +
  xlab("Country") + ylab("ET1") +
  coord_flip() # save 1500x10000

#one solution could be 
#scale_fill_continuous(aes(as.numeric(band)),breaks = c(500, 1000)) # band instead of upbound # doesn’t work neither 

这里 ex_a 图ex_a

问题: 1. 值没有按预期下降。2. 1200 个堆叠值的颜色看起来不太好。3. 堆叠应按波段而不是上行。4. 为了获得更好的可视化效果,我认为 ET1 有一个对数刻度,但两者都不起作用。5. 翻转后,图表应该是国家 vs ET1,而不是国家 vs 上行。

这里有一个可重现的例子:example

我将非常感谢任何帮助。

标签: rggplot2dplyr

解决方案


我不确定这是您正在寻找问题的结果 1。如果我弄错了,请通过评论告诉我。但我推断您想按照每个 ISO3 的案例数对 x 轴进行排序。我在这里做了一个很大的假设,这可能是错误的,您希望 x 轴在所有具有共同 ISO3 值的观察中通过 ET1 的最高值进行排序。

 library(tidyverse)

 ex1 %>% 
 group_by(ISO3) %>% 
 mutate(ET1_sort = max(ET1)) %>% ## Create a value through which to sort the x axis in the geom_bar()
 ggplot() +
 geom_bar(aes(x = reorder(ISO3, X = ET1_sort), ## Sort here, through reorder
          fill = as.factor(upbound))) + #use as.factor to stack (correct?) //R I think so
 theme(legend.position="none") + # none because there are 1200 values in legend
 xlab("Country") + 
 ylab("ET1") + ## Watch out, this might or might not be representative of ET1. The stack is a sum of observations, which does not necesarily reflect the ET1 values from your df. Again, check if this is true or not.
 coord_flip()

结果:

图

或者,在这里我对观察的数量进行排序。这是另一个大假设。

ex1 %>%
group_by(ISO3) %>%
mutate(ET1_sort = n()) %>% ## Create a value through which to sort the x axis in the geom_bar()
ggplot() +
geom_bar(aes(x = reorder(ISO3, X = ET1_sort), ## Sort here, through reorder
             fill = as.factor(upbound))) + #use as.factor to stack (correct?) //R I think so
theme(legend.position="none") + # none because there are 1200 values in legend
xlab("Country") + 
ylab("ET1") + ## Watch out, this might or might not be representative of ET1. The stack is a sum of observations, which does not necesarily reflect the ET1 values from your df. Again, check if this is true or not.
coord_flip()

结果2:

结果2


推荐阅读