【问题标题】:ggplot2 is not plotting two groupsggplot2 没有绘制两组
【发布时间】:2018-03-13 14:35:21
【问题描述】:

我现在脑子里放了个屁。为什么以下代码没有为变量Ford 的每个类别绘制两行?在我看来,我将变量映射为分组变量,然后在 geom_point()geom_line() 中映射为美学(col)。我觉得我只是忽略了一些非常基本的东西。

#libraries
library(tidyverse)
#data
structure(list(stressx = c(0, 0.33, 0.67, 1, 0, 0.33, 0.67, 1, 
0, 0.33, 0.67, 1, 0, 0.33, 0.67, 1), visiblex = c(0, 0, 0, 0, 
1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1), ford = c(0, 0, 0, 0, 0, 
0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1), preds = c(0.166275511711196, 
0.25404479263251, 0.344473748733258, 0.432243029654572, 0.417891216538386, 
0.449861131692899, 0.482799832155125, 0.514769747309638, 0.166275511711196, 
0.25404479263251, 0.344473748733258, 0.432243029654572, 0.417891216538386, 
0.449861131692899, 0.482799832155125, 0.514769747309638), se.fit =             
c(0.0216850668407667, 
0.0140669010411715, 0.014932848560481, 0.0233259879905658,         
0.0546688696666978, 
0.034867400606124, 0.0282122239553816, 0.0418803253364085,     
0.0216850668407667, 
0.0140669010411715, 0.014932848560481, 0.0233259879905658, 
0.0546688696666978, 
0.034867400606124, 0.0282122239553816, 0.0418803253364085)), .Names = 
c("stressx", 
"visiblex", "ford", "preds", "se.fit"), out.attrs = structure(list(
dim = structure(c(4L, 2L, 2L), .Names = c("stressx", "visiblex", 
"ford")), dimnames = structure(list(stressx = c("stressx=0.0000000", 
"stressx=0.3333333", "stressx=0.6666667", "stressx=1.0000000"
), visiblex = c("visiblex=0", "visiblex=1"), ford = c("ford=0", 
"ford=1")), .Names = c("stressx", "visiblex", "ford"))), .Names = c("dim", 
"dimnames")), row.names = c(NA, -16L), class = "data.frame")`

我的剧情

newdat %>% 
mutate(visiblex=recode_factor(visiblex, `0`="Not Visible Minority",     
`1`="Visible Minority"), ford=recode_factor(ford, `0`="Disapprove",    
`1`="Approve"), stressx=recode_factor(stressx, `0`='Strongly disagree',     
`0.33`='Somewhat disagree', `0.67`='Somewhat agree', `1`='Strongly agree')) 
%>% 
rename(Stress=stressx, Visible=visiblex, Ford=ford, Prob=preds) %>% 
#filter(Ford=='Approve') %>% 
ggplot(., aes(x=Stress, y=Prob, group=Ford))+ 
geom_point(aes(col=Ford))+
geom_line(aes(col=Ford))+
facet_wrap(~Visible)+
ylim(c(0,1))+
theme(axis.text.x=element_text(angle=45, vjust=0.5))`

【问题讨论】:

    标签: r ggplot2 tidyverse


    【解决方案1】:

    这是因为对于因子变量 Ford 的两个水平,您都有相同的数据点。我稍微修改了您的代码以显示数据,然后使用geom_jitter 而不是geom_point 绘制数据,现在您可以看到这两个数据点。由于底层数据点是相同的,通过这些数据点绘制的线也是重叠的,只有其中一个是可见的。

    #libraries
    library(tidyverse)
    #data
    newdat <- structure(
      list(
        stressx = c(0, 0.33, 0.67, 1, 0, 0.33, 0.67, 1,
                    0, 0.33, 0.67, 1, 0, 0.33, 0.67, 1),
        visiblex = c(0, 0, 0, 0,
                     1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1),
        ford = c(0, 0, 0, 0, 0,
                 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1),
        preds = c(
          0.166275511711196,
          0.25404479263251,
          0.344473748733258,
          0.432243029654572,
          0.417891216538386,
          0.449861131692899,
          0.482799832155125,
          0.514769747309638,
          0.166275511711196,
          0.25404479263251,
          0.344473748733258,
          0.432243029654572,
          0.417891216538386,
          0.449861131692899,
          0.482799832155125,
          0.514769747309638
        ),
        se.fit =
          c(
            0.0216850668407667,
            0.0140669010411715,
            0.014932848560481,
            0.0233259879905658,
            0.0546688696666978,
            0.034867400606124,
            0.0282122239553816,
            0.0418803253364085,
            0.0216850668407667,
            0.0140669010411715,
            0.014932848560481,
            0.0233259879905658,
            0.0546688696666978,
            0.034867400606124,
            0.0282122239553816,
            0.0418803253364085
          )
      ),
      .Names =
        c("stressx",
          "visiblex", "ford", "preds", "se.fit"),
      out.attrs = structure(
        list(
          dim = structure(c(4L, 2L, 2L), .Names = c("stressx", "visiblex",
                                                    "ford")),
          dimnames = structure(
            list(
              stressx = c(
                "stressx=0.0000000",
                "stressx=0.3333333",
                "stressx=0.6666667",
                "stressx=1.0000000"
              ),
              visiblex = c("visiblex=0", "visiblex=1"),
              ford = c("ford=0",
                       "ford=1")
            ),
            .Names = c("stressx", "visiblex", "ford")
          )
        ),
        .Names = c("dim",
                   "dimnames")
      ),
      row.names = c(NA, -16L),
      class = "data.frame"
    )
    
    #my plot
    data <- newdat %>%
      mutate(
        visiblex = recode_factor(visiblex, `0` = "Not Visible Minority",
                                 `1` = "Visible Minority"),
        ford = recode_factor(ford, `0` = "Disapprove",
                             `1` =
                               "Approve"),
        stressx = recode_factor(
          stressx,
          `0` = 'Strongly disagree',
          `0.33` =
            'Somewhat disagree',
          `0.67` = 'Somewhat agree',
          `1` = 'Strongly agree'
        )
      ) %>%
      dplyr::rename(
        Stress = stressx,
        Visible = visiblex,
        Ford = ford,
        Prob = preds
      ) 
    
    # display data
    data
    #>               Stress              Visible       Ford      Prob     se.fit
    #> 1  Strongly disagree Not Visible Minority Disapprove 0.1662755 0.02168507
    #> 2  Somewhat disagree Not Visible Minority Disapprove 0.2540448 0.01406690
    #> 3     Somewhat agree Not Visible Minority Disapprove 0.3444737 0.01493285
    #> 4     Strongly agree Not Visible Minority Disapprove 0.4322430 0.02332599
    #> 5  Strongly disagree     Visible Minority Disapprove 0.4178912 0.05466887
    #> 6  Somewhat disagree     Visible Minority Disapprove 0.4498611 0.03486740
    #> 7     Somewhat agree     Visible Minority Disapprove 0.4827998 0.02821222
    #> 8     Strongly agree     Visible Minority Disapprove 0.5147697 0.04188033
    #> 9  Strongly disagree Not Visible Minority    Approve 0.1662755 0.02168507
    #> 10 Somewhat disagree Not Visible Minority    Approve 0.2540448 0.01406690
    #> 11    Somewhat agree Not Visible Minority    Approve 0.3444737 0.01493285
    #> 12    Strongly agree Not Visible Minority    Approve 0.4322430 0.02332599
    #> 13 Strongly disagree     Visible Minority    Approve 0.4178912 0.05466887
    #> 14 Somewhat disagree     Visible Minority    Approve 0.4498611 0.03486740
    #> 15    Somewhat agree     Visible Minority    Approve 0.4827998 0.02821222
    #> 16    Strongly agree     Visible Minority    Approve 0.5147697 0.04188033
    
    # plot the data
    data %>%
      #filter(Ford=='Approve') %>%
      ggplot2::ggplot(data = .,
                      mapping = aes(x = Stress, y = Prob, group = Ford, colour = Ford)) +
      ggplot2::geom_jitter() + # change this back geom_point()
      ggplot2::geom_line() +
      ggplot2::facet_wrap( ~ Visible) +
      ggplot2::scale_y_continuous(limits = c(0, 1)) +
      ggplot2::theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
    

    reprex package (v0.2.0) 于 2018 年 3 月 13 日创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      相关资源
      最近更新 更多