首页 > 解决方案 > 如何更改 x 轴标签?

问题描述

这是我用来尝试获取附件中的输出的代码:

fun_plot5 <- function(ycol, ylab, xcol, data) {
  xx3 <- paste(ycol, xcol, sep = "~")
  xx3 <- as.formula(xx3)
  plotmeans(xx3, data = get_proposer,
            xlab = "Gender", ylab = ylab,
            main = "Mean Plot with 95% CI")
}

y_cols6 <- names(get_proposer[24:29])

y_lab6 <- c("Actual Offer (by A)", "Actual Amount Transferred to Partner (Bot)", "Actual Payoff (for A)", "Practice Offer (by A)", "Pradtice Amount Transferred to Partner (Bot)", "Practice Payoff (for A)")

old_par4 <- par(mfrow = c(3,3))
mapply(fun_plot5, y_cols6, y_lab6, 
       MoreArgs = list(
         xcol = "gender",
         data = get_proposer
       ))

在此处输入图像描述

我正在尝试将 x 轴值(对于所有图)从 1 和 2 分别更改为“男性”和“女性”。我尝试在上面代码的末尾包含这行代码,但我仍然无法得到我想要的结果。

fun_plot5 +
  scale_x_discrete(limits = c("Male", "Female"))

当我将此行添加到使用 ggplot 的其他绘图之一时,它起作用了。但它不适用于当前的情节,附后。我该怎么做呢?

非常感谢!

已更新数据

# A tibble: 31 x 10
   similar_task   age gender income actual_offer actual_payoff actual_partner_transfer practice_partner_transfer practice_offer practice_payoff
          <dbl> <dbl>  <dbl>  <dbl>        <dbl>         <dbl>                   <dbl>                     <dbl>          <dbl>           <dbl>
 1            5    29      1      4           40           126                      66                        48             30             118
 2            3    36      1      4          100           273                     273                       180            100             180
 3            5    39      2      2            0           100                       0                         0              0             100
 4            3    25      1      7          100             6                       6                       195            100             195
 5            3    28      2      7           25            99                      24                        84             50             134
 6            2    45      2      5           80            29                       9                        42            100              42
 7            3    30      1      6          100            45                      45                       123            100             123
 8            5    37      1      3            0           100                       0                         0              0             100
 9            2    38      2      2           25            99                      24                        63             25             138
10            1    25      1      1          100           183                     183                       285            100             285
# ... with 21 more rows

我在绘图中使用的列(附件)可以在数据的最后几列中找到,从“actual_offer”到“practice_payoff”(或者,整个数据集中的第 24:29 列)。

标签: rplot

解决方案


在 plotmeans 文档中,legends 参数被定义为包含用于标记组的字符串的向量。当您在 plotmeans 调用中尝试 legends=c("Male, "Female") 时会发生什么?


推荐阅读