ggplot2:如何交换Y轴和右侧刻面条以及如何订购刻面值 [英] ggplot2: How to swap y-axis and right facet strip and how to order facet values

查看:427
本文介绍了ggplot2:如何交换Y轴和右侧刻面条以及如何订购刻面值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

  library(ggplot2)
set.seed(6809)
钻石< - 钻石[样品(nrow(钻石),1000),]
钻石$ cut < - 因子(钻石$ cut,
levels = c(Ideal,Very Good, (钻石,aes(克拉,..density ..))重复第一个例子
p< - ggplot +
geom_histogram(binwidth = 1)
p + facet_grid(color_cut)

我可以创建下面的图:


  1. 如何排序根据我想要的顺序剥离
    例如(G,F,D,E,I,J,H)?

  2. 如何将右边的条带换成y轴(密度)? $ b


解决方案

ggplot2 2.2.1更新



使用ggplot2版本2,可以切换轴标签和刻面标签的位置。所以这里是更新后的代码,它们充分利用了这些功能:

 #重新排序因子等级
钻石$ color = factor(diamonds $ color,levels = c(G,F,D,E,I,J,H))

ggplot(diamonds,aes carat,..density ..))+
geom_histogram(binwidth = 1)+
facet_grid(color_cut,switch =y)+#将y个小平面条放在左边
scale_y_continuous(density,position =right)+#将y轴标签放在右边
主题(strip.text.y = element_text(angle = 180))



Original Answer

正如@joran所说,如果你想完全控制在哪里,你必须修改网格对象。这很痛苦。

这是另一种仍然很麻烦的方法,但比修改网格对象更容易(至少对我来说)。其基本思想是,我们定位各个小平面和轴标签,以便我们可以将曲线图逆时针旋转90度(以获取左侧的小平面标签),同时仍然可以将所有标签正确定向。

为了达到这个目的,你需要用几种方式修改图表:注意我添加了 coord_flip ,全部主题的东西,和 scale_x_reverse 。还要注意我已经切换了facet变量的顺序,所以 color >从最上面开始(在我们旋转图之后它会在左边)。

 #重新排序因子等级
钻石$ color = factor(钻石$ color,levels = rev(c(G ,F,D,E,I,J,H)))

p < - ggplot(钻石,aes(克拉, ..))+
geom_histogram(binwidth = 1)+
facet_grid(cut〜color)+ coord_flip()+
theme(strip.text.x = element_text(angle = -90) ,
axis.text.y = element_text(angle = -90,vjust = 0.5,hjust = 0.5),
axis.text.x = element_text(angle = -90,vjust = 0.5,hjust = 0),
axis.title.x = element_text(angle = 180),
axis.title.y = element_text(angle = -90))+
scale_x_reverse()

一个选项是保存图形,然后在另一个程序中旋转它(例如预览,如果您在苹果电脑)。不过,在这个SO答案的帮助下,我能够在R.它需要一些试验和错误(我有限的如何操作网格对象的知识)才能为视口获得正确的大小。我将它保存为PNG以张贴在SO上,但您当然可以将它保存为PDF,这看起来更好。

  png(example.png,500,600)
pushViewport(viewport(width = unit(8,inches ),height = unit(7,inches)))
print(p,vp = viewport(angle = 90))
dev.off()

结果如下:


With the following code:

library(ggplot2)
set.seed(6809)
diamonds <- diamonds[sample(nrow(diamonds), 1000), ]
diamonds$cut <- factor(diamonds$cut,
         levels = c("Ideal", "Very Good", "Fair", "Good", "Premium"))

# Repeat first example with new order
p <- ggplot(diamonds, aes(carat, ..density..)) +
        geom_histogram(binwidth = 1)
p + facet_grid(color ~ cut)

I can create the following figure:

My questions are:

  1. How can I sort the right strip according to my desired order e.g. (G, F, D, E, I, J, H)?
  2. How can I swap the right strip to with the y-axis (density)?

解决方案

Update for ggplot2 2.2.1

With ggplot2 version 2, you can switch the positions of the axis labels and facet labels. So here's updated code that takes advantage of these features:

# Reorder factor levels
diamonds$color = factor(diamonds$color, levels=c("G","F","D","E","I","J","H"))

ggplot(diamonds, aes(carat, ..density..)) +
  geom_histogram(binwidth=1) +
  facet_grid(color ~ cut, switch="y") +   # Put the y facet strips on the left
  scale_y_continuous("density", position="right") +   # Put the y-axis labels on the right
  theme(strip.text.y=element_text(angle=180))

Original Answer

As @joran said, you have to modify the grid object if you want full control over what goes where. That's painful.

Here's another approach that's still a hassle, but easier (for me at least) than modifying the grid object. The basic idea is that we orient the various facet and axis labels so that we can rotate the plot 90 degrees counter-clockwise (to get the facet labels on the left side) while still having all the labels oriented properly.

To make this work, you need to modify the graph in several ways: Note my addition of coord_flip, all the theme stuff, and scale_x_reverse. Note also that I've switched the order of the facet variables, so that color starts out on top (it will be on the left after we rotate the graph).

# Reorder factor levels
diamonds$color = factor(diamonds$color, levels=rev(c("G","F","D","E","I","J","H")))

p <- ggplot(diamonds, aes(carat, ..density..)) +
  geom_histogram(binwidth = 1) +
  facet_grid(cut ~ color) + coord_flip() +
  theme(strip.text.x=element_text(angle=-90),
        axis.text.y=element_text(angle=-90, vjust=0.5, hjust=0.5),
        axis.text.x=element_text(angle=-90, vjust=0.5, hjust=0),
        axis.title.x=element_text(angle=180),
        axis.title.y=element_text(angle=-90)) +
  scale_x_reverse()

One option is to save the graph and then rotate it in another program (such as Preview, if you're on a Mac). However, with the help of this SO answer, I was able to rotate the plot within R. It required some trial and error (with my limited knowledge of how to manipulate grid objects) to get the right size for the viewport. I saved it as a PNG for posting on SO, but you can of course save it as a PDF, which will look nicer.

png("example.png", 500,600)
pushViewport(viewport(width = unit(8, "inches"), height = unit(7, "inches"))) 
print(p, vp=viewport(angle=90))
dev.off()

And here's the result:

这篇关于ggplot2:如何交换Y轴和右侧刻面条以及如何订购刻面值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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