facet_zoom正在缩放到不同的x轴值

时间:2017-02-24 02:44:45

标签: r ggplot2 ggforce

使用此

数据

df <- read.table(text = c("
Site    measured    simulated
site1   9.1 6.8
                          site2 163.1   128.1
                          site3 126 75.9
                          site4 741.2   843
                          site5 2215.1  1937.7
                          site6 283.6   423.4
                          site7 115.6   92.5
                          site8 12.1    15.3
                          site9 13.4    15.8
                          site10    475.7   296.1
                          site11    1   1.4
                          site12    84.5    131.9
                          site13    74.1    43.9
                          site14    19.2    33.3
                          site15    74.8    41.1
                          site16    287.8   366.9"), header =T)

和下面的脚本

library(tidyr) 
library(dplyr)
library(ggplot2)
library(ggforce)
df_reorder <-  transform(df, 
                         Site_reorder = reorder(Site, -measured))
df_long <- df_reorder %>% gather("id", "value", 2:3) 


ggplot(df_long, aes(x = Site_reorder, y = value, fill = id))+
  geom_bar(stat = "identity", position = "dodge", width = 0.7)+
  scale_fill_manual(values=c("red", "black")) + 
  theme_bw()+
  facet_zoom(x = Site_reorder%in%c("site14", "site9", "site8", "site1", "site11"))+
  labs(x = "", y = " ")+
  theme(legend.position = c(0.90, 0.90), legend.background = element_rect(fill="transparent"),
        legend.title = element_blank(), axis.text.x=element_text(angle=55, vjust=1,  hjust=1,size = 8))

我希望缩放适用于最后五个网站(site14site9site8site1site11)。相反,它会放大到前五个网站(site5site4site10site16site6

enter image description here

有关如何对最近五个网站进行缩放的任何建议都会受到赞赏吗?

2 个答案:

答案 0 :(得分:4)

ggforce仅缩放连续比例。

1-您可以使用as.numeric将级别转换为数字,

2-然后使用scale_x_continous打印网站名称

ggplot(df_long, aes(x = as.numeric(Site_reorder), y = value, fill = id))+
  geom_bar(stat = "identity", position = "dodge", width = 0.7)+
  scale_fill_manual(values=c("red", "black")) + 
  theme_bw()+
  facet_zoom(xy = Site_reorder%in%c("site14", "site9", "site8", "site1", "site11"), horizontal=FALSE) + 
  scale_x_continuous(
    breaks = 1:length(levels(df_long$Site_reorder)),
    label = levels(df_long$Site_reorder)
  )+
  labs(x = "", y = " ")+
  theme(legend.position = c(0.90, 0.90), legend.background = element_rect(fill="transparent"),
        legend.title = element_blank(), axis.text.x=element_text(angle=55, vjust=1,  hjust=1,size = 8))

enter image description here

答案 1 :(得分:0)

所有功劳归@HubertL,因为这完全基于他的解决方案。我已将其设置为最小,因此可以更轻松地进行跟踪

df_reorder <-  transform(df, Site_reorder = reorder(Site, -measured))
df_long <- df_reorder %>% gather("id", "value", 2:3)

b <- df_long %>% 
  filter(id == "measured") %>% 
  select(-Site, -id) %>% as.tibble()


# b is just a minimal version of df_long
b

b %>% 
  ggplot(aes(x = as.numeric(Site_reorder), y = value)) +
  geom_bar(stat = "identity", width = 0.7) +
  facet_zoom(xy = Site_reorder %in% c("site14", "site9", "site8", "site1", "site11"), horizontal=FALSE) + 
  scale_x_continuous(
    breaks = 1:length(levels(df_long$Site_reorder)),
    label = levels(df_long$Site_reorder)
  )