gpt4 book ai didi

r - facet_zoom 正在放大到不同的 x 轴值

转载 作者:行者123 更新时间:2023-12-04 01:47:54 24 4
gpt4 key购买 nike

使用这个

数据

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)。相反,它放大了前五个站点(site5site4site10site16站点 6)

enter image description here

如有任何关于如何对最后五个站点进行缩放的建议,我们将不胜感激?

最佳答案

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

关于r - facet_zoom 正在放大到不同的 x 轴值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42429864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com