订购了ggplot2 dotplot with facet [英] ordered ggplot2 dotplot with facet

查看:181
本文介绍了订购了ggplot2 dotplot with facet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在复制此dotplot的一个版本 http:// blogs.bgsu.edu/math6820cwenren/files/2011/04/ggplot-dotplot.png from http://blogs.bgsu.edu/math6820cwenren/2011/04/27/ggplot2/ 。代码如下。



我希望剧情和按照演示顺序进行匹配。这可以通过对变量ind进行重新排序来实现,如曲线中所示,即Tracy,LeBron,Kobe,Anthony。反之亦然,然后重新排列密钥。我尝试用几种方法重新排序因素,但按键顺序总是颠倒剧情中的顺序。

如何重新排序,使演示文稿一致?

 科比= c(31.6,28.3,26.8,27.0,24.8)
LeBron = c(27.3,30.0, 28.4,29.7,26.0)
特雷西= c(24.6,21.6,15.6,8.6,8.5)
安东尼= c(28.9,25.7,22.8,28.2,25.3)
年= c 2006年,2007年,2008年,2009年,2010年)

data1 = cbind(科比,勒布朗,特雷西,安东尼)

data = data.frame(data1,row.names =年)

d = data.frame(Year = dimnames(data)[[1]],stack(data))

p = ggplot(d,aes(values,ind ,color = ind))
p + geom_point()+ facet_wrap(〜Year,ncol = 1)


解决方案

您可以使用因子和一个指定级别顺序的字符向量来命令级别:

  d $ ind < -  factor(d $ ind,levels = unique(d $ ind))

这里, unique(d $ ind)以数据框中出现的顺序返回值。这是颜色比例的级别顺序。



您想为 y 轴。默认情况下,订单是从下到上。您可以创建第二个变量 ind2 ,并使用颠倒的顺序:

  d $ ind2 < - 因子(d $ ind,levels = rev(水平(d $ ind)))

这将被用作 y 轴的变量。

  library(ggplot2)
ggplot(d,aes(x = values,y = ind2,color = ind))+
geom_point()+ facet_wrap(〜Year,ncol = 1)


I am replicating a version of this dotplot http://blogs.bgsu.edu/math6820cwenren/files/2011/04/ggplot-dotplot.png from http://blogs.bgsu.edu/math6820cwenren/2011/04/27/ggplot2/. Code is below.

I would like the plot and the key to match in the order of presentation. This could be accomplished by reordering the variable ind to appear as in the plot, i.e., Tracy, LeBron, Kobe, Anthony. Or vice-versa and reorder the key. I've tried reordering the factors with several methods but the order in key is always reverse the order in the plot.

How to reorder so the presentation is consistent?

Kobe=c(31.6, 28.3, 26.8, 27.0, 24.8)
LeBron=c(27.3, 30.0, 28.4, 29.7, 26.0)
Tracy=c(24.6, 21.6, 15.6, 8.6, 8.5)
Anthony=c(28.9, 25.7, 22.8, 28.2, 25.3)
year=c(2006, 2007, 2008, 2009, 2010)

data1=cbind(Kobe, LeBron, Tracy, Anthony)

data=data.frame(data1,row.names=year)

d=data.frame(Year=dimnames(data)[[1]],stack(data))

p=ggplot(d, aes(values, ind, color=ind))
p + geom_point()+ facet_wrap(~Year, ncol=1)

解决方案

You can order the levels with factor and a character vector specifying the order of levels:

d$ind <- factor(d$ind, levels = unique(d$ind))

Here, unique(d$ind) returns the values in the order of appearance in the data frame. This is the order of levels for the color scale.

You want a reversed order of levels for the y axis. By default, the order is bottom to top. You can create a second variable, ind2, with reversed level order:

d$ind2 <- factor(d$ind, levels = rev(levels(d$ind)))

This will be used as the variable for the y axis.

library(ggplot2)
ggplot(d, aes(x = values, y = ind2, color = ind)) +
  geom_point() + facet_wrap(~ Year, ncol = 1)

这篇关于订购了ggplot2 dotplot with facet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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