首页 文章

ggplot2 - 使用点划线创建图形“错误:连续变量无法映射到线型”

提问于
浏览
2

我正在尝试创建一个类似于这个的图形 .

Screen Shot

但是,我收到错误“错误:连续变量无法映射到线型”

这是我的代码:

combined.dframe <- data.frame(
  "Sample" = c("Share of U.S. Total Adult Population", "Share of Prison Population", 
               "Share of U.S. Total Adult Population", "Share of Prison Population", 
               "Share of U.S. Total Adult Population", "Share of Prison Population"), 
  "Race" = c("White","White", "Black", "Black", "Hispanic", "Hispanic"), 
  "Percentage" = c(60.7, 27.4, 13.4, 37.0, 18.1, 32.6))
combined.dframe$Sample <- factor(combined.dframe$Sample, 
                                 levels = c("Share of U.S. Total Adult Population", 
                                            "Share of Prison Population"))

compare.plot <- ggplot(data = combined.dframe, 
                       aes(x = Sample, y = Percentage, group = Race)) +
  geom_line(aes(color = Race, linetype = 4, alpha = 1), size = 2) +
  geom_point(aes(color = Race, alpha = 1), size = 3.15) +
  geom_text(data = combined.dframe %>% 
              filter(Sample == "Share of U.S. Total Adult Population"), 
            aes(label = paste0(Race, " - ", Percentage, "%")) , 
            hjust = 1.35, fontface = "bold", size = 3.15) +
  geom_text(data = combined.dframe %>% 
              filter(Sample == "Share of Prison Population"), 
            aes(label = paste0(Race, " - ", Percentage, "%")) , 
            hjust = -.35, fontface = "bold", size = 3.15) +
  theme_bw() +
  scale_x_discrete(position = "top") +
  theme(panel.border     = element_blank()) +
  theme(axis.title.y     = element_blank()) +
  theme(axis.text.y      = element_blank()) +
  theme(panel.grid.major.y = element_blank()) +
  theme(panel.grid.minor.y = element_blank()) +
  theme(axis.title.x     = element_blank()) +
  theme(panel.grid.major.x = element_blank()) +  
  theme(axis.ticks       = element_blank()) +
  theme(legend.position = "none") +
  theme(panel.border     = element_blank()) +
  theme(plot.title       = element_text(size= 12, face = "bold", hjust = 0.5)) +
  theme(plot.subtitle       = element_text(size=7)) +
  theme(plot.caption       = element_text(size=5)) +
  labs(
    title = "Overrepresentation Of Minority Races In Prison",
    subtitle = "The racial makeup of U.S. prisons looks substantially different from the demographics of the country as a whole.",
    caption = "Source: U.S. Census Bureau, 2016"
  )

这是没有线型的图表 .

Screen shot

1 回答

  • 1

    linetype=4 参数放在 geom_line(aes()) 位之外,它会打印出没有问题和虚线的图形 . 新代码:

    compare.plot <- ggplot(data = combined.dframe, aes(x = Sample, y = Percentage, group = Race)) +
       geom_line(aes(color = Race, alpha = 1), size = 2, linetype = 4) +
       geom_point(aes(color = Race, alpha = 1), size = 3.15) +
       geom_text(data = combined.dframe %>% filter(Sample == "Share of U.S. Total Adult Population"), 
                 aes(label = paste0(Race, " - ", Percentage, "%")) , 
                 hjust = 1.35, 
                 fontface = "bold", 
                 size = 3.15) +
       geom_text(data = combined.dframe %>% filter(Sample == "Share of Prison Population"), 
                 aes(label = paste0(Race, " - ", Percentage, "%")) , 
                 hjust = -.35, 
                 fontface = "bold", 
                 size = 3.15) +
       theme_bw() +
       scale_x_discrete(position = "top") +
       theme(panel.border     = element_blank()) +
       theme(axis.title.y     = element_blank()) +
       theme(axis.text.y      = element_blank()) +
       theme(panel.grid.major.y = element_blank()) +
       theme(panel.grid.minor.y = element_blank()) +
       theme(axis.title.x     = element_blank()) +
       theme(panel.grid.major.x = element_blank()) +  
       theme(axis.ticks       = element_blank()) +
       theme(legend.position = "none") +
       theme(panel.border     = element_blank()) +
       theme(plot.title       = element_text(size= 12, face = "bold", hjust = 0.5)) +
       theme(plot.subtitle       = element_text(size=7)) +
       theme(plot.caption       = element_text(size=5)) +
       labs(
         title = "Overrepresentation Of Minority Races In Prison",
         subtitle = "The racial makeup of U.S. prisons looks substantially different from the demographics of the country as a whole.",
         caption = "Source: U.S. Census Bureau, 2016"
       )
    

相关问题