ggplot2中每个面的不同轴限制 [英] Different axis limits per facet in ggplot2

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

问题描述

我试图在 ggplot2 中创建一个分面图,其中y轴显示标签,x轴应在两个不同的度量中显示每个标签值的线图(它们在不同的尺度上)。到目前为止,我有这样的:

  Data<  -  structure(list(label = structure(
c(1L,1L ,2L,2L,3L,3L,4L,
4L,5L,5L,6L,6L),标签= c(A,B,C,D ,F),class =factor),
facet = structure(c(1L,2L,1L,2L,1L,2L,1L,2L,1L,2L,
1L,2L ),.Label = C( A, b)中,class = 因素),值= C(0.0108889081049711,
0.37984336540103,0.0232500876998529,0.777756493305787,
0.0552913920022547,0.920194681268185,0.0370863009011373,
0.114463779143989,0.00536034172400832,0.469208759721369,
0.0412159096915275,0.587875489378348),基团= C(1,1,1,
1,1,1,1,1,1,1,1,1) ),.Names = c(label,facet,
value,group),row.names = c(NA,-12L),class =data.frame)

ggplot(data,aes(x = label,y = value,group = group))+ geom_line()+
facet_grid(〜facet,scales =free)+ coord_flip()

创建如下图:
< img src =https://i.stack.imgur.com/R07p7.pngalt =在这里输入图片描述>



问题是,是在不同的尺度上,我更喜欢 A 图的X限制从0到0.1, B 绘图为我认为 scales =free应该解决这个问题,但它不会改变图。

df239 的东西:



ggplot(Data,aes(y = label,x = value,group = group))+ geom_path()+
facet_wrap(〜facet,scales =free)

请注意,您必须使用 geom_path ,并且注意点数的排序,因为只是切换 x y coord_flip (正如在其他答案中指出,不支持 facet_wrap )。


I am trying to make a faceted plot in ggplot2 where the y axis shows labels and the x axis should show line graphs with the value for each label in two different measures (which are on different scales). So far I have this:

Data <- structure(list(label = structure(
  c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
  4L, 5L, 5L, 6L, 6L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), 
  facet = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
  1L, 2L), .Label = c("A", "B"), class = "factor"), value = c(0.0108889081049711, 
  0.37984336540103, 0.0232500876998529, 0.777756493305787, 
  0.0552913920022547, 0.920194681268185, 0.0370863009011373, 
  0.114463779143989, 0.00536034172400832, 0.469208759721369, 
  0.0412159096915275, 0.587875489378348), group = c(1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("label", "facet", 
  "value", "group"), row.names = c(NA, -12L), class = "data.frame")

ggplot(Data, aes(x = label, y = value, group = group)) + geom_line() + 
     facet_grid(~ facet, scales = "free") + coord_flip()

Which creates the following plot:

The problem is that the measures are on different scales and I would prefer the A plot to have x limits from 0 to 0.1 and the B plot to have x limits from 0 to 1. I thought scales = "free" should fix this but it doesn't change the plot.

解决方案

I came up with something similar to df239:

ggplot(Data, aes(y = label, x = value, group=group)) + geom_path() + 
  facet_wrap( ~ facet, scales = "free")

Note you have to use geom_path, and take care with the ordering of your points because just switching x and y is not the same as coord_flip (which as noted in the other answer isn't supported with facet_wrap).

这篇关于ggplot2中每个面的不同轴限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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