ggplot2-创建带有点破折号线型的图形“错误:连续变量无法映射到线型”

时间:2018-12-10 05:35:01

标签: r ggplot2

我正在尝试创建与此图相似的图。

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 个答案:

答案 0 :(得分: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"
   )