gpt4 book ai didi

r - 更改ggplot2中构面图的每一行的y轴限制

转载 作者:行者123 更新时间:2023-12-04 15:33:39 25 4
gpt4 key购买 nike

我有一个3行乘5列的平面图。每行显示分布在不同范围内的数据。为了正确显示数据以显示所有内容,我没有设置y轴限制。

这是我的代码:

require(reshape2)
library(ggplot2)
library(RColorBrewer)

fileName = paste("./data_test.csv", sep = "")

## data available here: https://dl.dropboxusercontent.com/u/73950/data_test.csv

mydata = read.csv(fileName,sep=",", header=TRUE)

dataM = melt(mydata,c("id"))
dataM = cbind(dataM,
colsplit(dataM$variable,
pattern = "_",
names = c("Network_model", "order", "category")))
dataM$variable <- NULL
dataM <- dcast(dataM, ... ~ category, value.var = "value")
dataM$minCut <- NULL
dataM$nbr_communities <- NULL
dataM$mean_community_size <- NULL
dataM$density <- NULL

my_palette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))

dataM = melt(dataM, id.vars = c("Network_model", "order", "nodesRemoved", "id"))

my_palette = c(brewer.pal(5, "Blues")[c(4)], brewer.pal(5, "Set1")[c(3)])

ggplot(dataM, aes(x= nodesRemoved ,y= value, group= order, color= order)) +
geom_point(size = .6,alpha = .15,position="jitter") + ## increased size
stat_smooth(se = FALSE, size = .5, alpha = .1, method = "loess") +
scale_color_manual(values=my_palette) +
theme_bw() +
theme(plot.background = element_blank(),
axis.line = element_blank(),
legend.key = element_blank(),
legend.title = element_blank(),
axis.text.x = element_text(size = 8),
axis.text.y = element_text(size = 8)
) +
scale_y_continuous("Value") +
scale_x_continuous("Time", limits=c(0, 100)) +

facet_grid(variable ~ Network_model,scales="free")

会产生以下结果:

现在,我想为三行中的每行有选择地设置限制,以便第一行是limits = c(1.9,3),第二行是limits = c(0,1),第三行是limits = c (.3,.7)。

如何在facep的ggplot2中实现此目标?

最佳答案

我认为您最好的选择是在绘制数据之前修剪数据,例如与dplyr

library(dplyr)
limits <- data.frame(variable = levels(dataM$variable),
min = c(1.9,0,0.3),
max = c(3,1,0.7))
dataC <- inner_join(dataM, limits) %>% filter(value > min, value < max)

last_plot() %+% dataC

(最初,我将分数提高了一点,以便更清楚地了解罪魁祸首)

关于r - 更改ggplot2中构面图的每一行的y轴限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299425/

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