ggplot2没有绘制两个组

时间:2018-03-13 14:35:21

标签: r ggplot2 tidyverse

我现在有一个完整的大脑屁。为什么下面的代码没有为变量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))`

1 个答案:

答案 0 :(得分:0)

这是因为因子变量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-03-13。