在ggplot2中的曲面图中绘制顺序 [英] Plotting order within a faceted dotchart in ggplot2

查看:119
本文介绍了在ggplot2中的曲面图中绘制顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在ggplot2中创建一个多面点图,但无法按照我想要的顺序显示面内
类别。绘制点阵图的代码是:

  g < -  ggplot(df2,aes(x = Y,y = label) )+ geom_point()
g <-g + facet_grid(激励〜。,scale =free)
g <-g + geom_errorbarh(aes(xmax = Y + se,xmin = Y-se ))
g < - g + geom_vline(xintercept = 1/6,linetype = 2,color =red)
g <-g + opts(title =%选择非焦点图像的主题,
strip.text.y = theme_text()
)+ xlab(%)+ ylab(Groups)
print(g)

该图的问题是,在仅限金钱方面,1美分和5美分类的顺序是错误的。这个问题似乎并不是这些因素本身的顺序,例如:

 > (df2 $ label)
[1]011 cent5 cent6
>



更新:排序因子不会改变图表顺序,即使用label3绘图,其中:


df2 $ label3
1 0 1 1 cent 5 cent 6

级别:0 < 1 < 1分< 5分< 6




 > str(df2 $ label3)
Ord.factor w / 5等级0<1<1美分<:1 2 3 4 5

实际数据帧:

  df2 < - 结构(list(Y = c(0.0869565217391304,0.114148148148148, 0.172413793103448,
0.384615384615385,0.5625),group = c(0L,1L,5L,3L,6L),se = c(0.0856368459098186,
0.079039229753282,0.0762650540661762,0.0805448741815074,0.0726021684593052
) = c(FALSE,TRUE,TRUE,TRUE,TRUE),incentive = structure(c(1L,
2L,3L,3L,4L),.Label = c(Default behavior,Imbalance only
Money only,Money& Imbalance together),class =factor),
label = structure(1:5,.Label = c(0,1, 1分,5分,
6),class =factor),plot_order = c(0,1,2,3,4)),.Names = c(Y ,
group,se,nudged,incentive,label,plot_order),
row.names = c(as.factor(group)0,
as.factor(group)1,as。因子(组)5,as.factor(group)3,
as.factor(group)6),class =data.frame)


解决方案

你的问题在于 facet_grid 激励类型('Default Behavior'等)的顺序与激励值的顺序冲突(0,1分,5分等) ),这就是为什么你没有在Money Only组中获得你想要的订单。解决这个问题的最简单方法是重新排序奖励因子,以便将默认行为绘制在底部而不是 top

  df2 $激励<  - 有序(df2 $激励,
级别= rev (c(默认行为,仅限不平衡,
仅货币,货币与不平衡在一起)))

并保留其余的代码。然后你得到这个图:


I'm trying to create a faceted dotchart in ggplot2 but cannot get the within-facet categories to appear in the order that I want. The code for plotting the dotchart is:

g <- ggplot(df2, aes(x=Y, y=label)) + geom_point() 
g <- g + facet_grid(incentive ~ .,   scale="free")
g <- g + geom_errorbarh(aes(xmax = Y + se, xmin = Y - se))  
g <- g + geom_vline(xintercept=1/6, linetype=2, colour="red") 
g <- g + opts(title="% Subjects Choosing Non-Focal Image",
          strip.text.y = theme_text()
          ) + xlab("%") + ylab("Groups")
print(g)

The problem with the plot is that within the "Money only" facet, the 1 cent and 5 cent categories are in the wrong order. The problem does not seem to be the order of the factors themselves, as:

> levels(df2$label)
[1] "0"      "1"      "1 cent" "5 cent" "6"     
> 

Update: ordering the factor does not appear to change the plot order, i.e., plotting with label3 where:

df2$label3 1 0 1 1 cent 5 cent 6
Levels: 0 < 1 < 1 cent < 5 cent < 6

>str(df2$label3)
Ord.factor w/ 5 levels "0"<"1"<"1 cent"<..: 1 2 3 4 5

The actual data frame:

df2 <- structure(list(Y = c(0.0869565217391304, 0.148148148148148, 0.172413793103448, 
0.384615384615385, 0.5625), group = c(0L, 1L, 5L, 3L, 6L), se = c(0.0856368459098186, 
0.079039229753282, 0.0762650540661762, 0.0805448741815074, 0.0726021684593052
), nudged = c(FALSE, TRUE, TRUE, TRUE, TRUE), incentive = structure(c(1L, 
2L, 3L, 3L, 4L), .Label = c("Default behavior", "Imbalance only", 
"Money only", "Money & Imbalance together"), class = "factor"), 
label = structure(1:5, .Label = c("0", "1", "1 cent", "5 cent", 
"6"), class = "factor"), plot_order = c(0, 1, 2, 3, 4)), .Names = c("Y", 
"group", "se", "nudged", "incentive", "label", "plot_order"), 
row.names = c("as.factor(group)0", 
"as.factor(group)1", "as.factor(group)5", "as.factor(group)3", 
"as.factor(group)6"), class = "data.frame")

解决方案

Your problem is that the facet_grid is plotting the incentive-types ('Default Behavior', etc) in an order that is conflicting with the order of the incentive-value (0, 1 cent, 5 cent, etc), which is why you are not getting the ordering you want within the Money Only group. The simplest way to fix it is to re-order your incentive factor, so that the Default Behavior is plotted at the bottom rather than the top:

df2$incentive <- ordered( df2$incentive,
                 levels = rev(c("Default behavior", "Imbalance only", 
                                "Money only", "Money & Imbalance together"))) 

and leave the rest of the code as-is. Then you get this plot:

这篇关于在ggplot2中的曲面图中绘制顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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