在 ggplot2 中的多面点图中绘制顺序

发布于 2024-10-13 04:38:10 字数 1905 浏览 3 评论 0原文

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

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)

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

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

alt text

更新 :对因子进行排序似乎不会改变绘图顺序,即使用 label3 绘图,其中:

df2$label3 1 0 1 1 分 5 分 6
级别:0 < 1 < 1分< 5分< 6

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

实际数据框:

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

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

alt text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

束缚m 2024-10-20 04:38:10

您的问题是,facet_grid 正在绘制激励类型(“默认行为”等),其顺序与 激励类型的顺序相冲突。值(0、1 美分、5 美分等),这就是您在“仅货币”组中无法获得所需订单的原因。解决此问题的最简单方法是重新排序您的激励因素,以便将默认行为绘制在底部而不是顶部 :

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

并将其余代码保留原样。然后你会得到这个情节:
替代文字

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:
alt text

甜点 2024-10-20 04:38:10

这听起来和看起来都很奇怪(因为标签是按因子水平的顺序排序的,正如 @Dirk Eddelbuettel 上面也写的那样),但你总是可以使用 scale_discrete

我根据您的代码编写了一个小示例,但这看起来也有点奇怪,请参阅:

g <- ggplot(df2, aes(x=Y, y=label)) + geom_point()
# add manual scale
g <- g+ scale_y_discrete(limits=c("0","1","1 cent","5 cent","6"))
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")

ggplot exampleplot

That sounds and looks really strange (as the labels are sorted by the order of factor levels, as @Dirk Eddelbuettel also wrote above), but you could always use scale_discrete.

I have made up a little example based on your code, but that looks also strange a bit, see:

g <- ggplot(df2, aes(x=Y, y=label)) + geom_point()
# add manual scale
g <- g+ scale_y_discrete(limits=c("0","1","1 cent","5 cent","6"))
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")

ggplot example plot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文