R ggplot2-使用点划线线型创建图形;错误:连续变量无法映射到线型;
我正在尝试创建一个类似于此的图形 但是,我得到了错误“error:连续变量不能映射到线型” 这是我的密码:R ggplot2-使用点划线线型创建图形;错误:连续变量无法映射到线型;,r,ggplot2,R,Ggplot2,我正在尝试创建一个类似于此的图形 但是,我得到了错误“error:连续变量不能映射到线型” 这是我的密码: 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 Pop
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"
)
combined.dframe将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"
)
compare.plot%过滤器(样本==“美国成年总人口的份额”),
aes(标签=0(种族,“-”,百分比,“%”),
hjust=1.35,
fontface=“bold”,
尺寸=3.15)+
geom_text(数据=组合的.dframe%>%过滤器(样本=“监狱人口份额”),
aes(标签=0(种族,“-”,百分比,“%”),
hjust=-0.35,
fontface=“bold”,
尺寸=3.15)+
主题_bw()+
比例x离散(位置=“顶部”)+
主题(panel.border=element\u blank())+
主题(axis.title.y=element\u blank())+
主题(axis.text.y=element\u blank())+
主题(panel.grid.major.y=element\u blank())+
主题(panel.grid.minor.y=element\u blank())+
主题(axis.title.x=element\u blank())+
主题(panel.grid.major.x=element_blank())+
主题(axis.ticks=element\u blank())+
主题(legend.position=“无”)+
主题(panel.border=element\u blank())+
主题(plot.title=element\u text(size=12,face=“bold”,hjust=0.5))+
主题(plot.subtitle=元素\文本(大小=7))+
主题(plot.caption=元素\文本(大小=5))+
实验室(
title=“监狱中少数族裔人数过多”,
subtitle=“美国监狱的种族构成与整个国家的人口统计数据有很大不同。”,
标题=“来源:美国人口普查局,2016年”
)