Ggplot2堆积图表,1200个值

时间:2018-06-02 15:20:37

标签: r ggplot2 dplyr

嗨ggplot2战士!

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

'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

#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.翻转后,图表应该是国家/地区与ET1,而不是国家/地区与上行。

这是一个可重现的例子:example

我非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

I am not sure this is the result you are looking for your question 1. Let me know through a comment if I am mistaken. But I inferred that you want to sort the x axis along the number of cases each ISO3 has. I am making the big assumption here, which might be wrong, that you want the x-axis sorted through the highest value of ET1 among all observation with a common ISO3 value.

 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()

Results:

The Figure

Alternatively, here I sort through the number of observations. This is another big assumption.

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()

Results2:

Results2