在ggplot2中更改构面图的每一行的y轴限制 [英] Change y axis limits for each row of a facet plot in ggplot2

查看:438
本文介绍了在ggplot2中更改构面图的每一行的y轴限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是我的代码:

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

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

##这里提供的数据: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)],bre​​wer。 pal(5,Set1)[c(3)])

ggplot(dataM,aes(x = nodesRemoved,y = value,group = order,color = order))+
geom_point(size = .6,alpha = .15,position =jitter)+ ##增加大小
stat_smooth(se = FALSE,size = .5,alpha = .1,method =黄土) +
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(变量〜Network_model,scales =free)

产生这种结果:


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

如何在ggplot2中实现这一点?

解决方案

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

  library(dplyr)
限制< - data.frame(variable = levels(dataM $ variable ),
min = c(1.9,0,0.3),
max = c(3,1,0.7))
dataC < - inner_join(dataM,limits)%>%过滤器(值> min,值
last_plot()%+%dataC



(我最初提出的观点更大,以更清楚地看到罪魁祸首)


I have a 3 rows by 5 columns facet plot. Each row show data which spread over different ranges. To properly display my data so everything is shown, I don't set a y axis limit.

Here's my code:

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

Which produces this:

Now, I'd like to selectively set limits for each of the three rows, so that the first row is limits=c(1.9, 3), the second is limits=c(0, 1) and the third is limits=c(.3, .7).

How can I achieve this in ggplot2 of faceting?

解决方案

I think your best option will be to trim the data before plotting it, e.g. with 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

(I initially made the points bigger to see the culprits more clearly)

这篇关于在ggplot2中更改构面图的每一行的y轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆