r - scale_shape_manual 不分配形状,为什么?
问题描述
我想为 A、B、C 绘制不同的形状,但我无法开始scale_shape_manual
工作。
Location = c(1,2,3,4,5,6,7)
A = c(1.16, 0.96, 0.67,0.78, 0.55, 0.3,0.26)
B = c(6.51, 4.98, 2.85, 3.19, 3.60, 10.82, 8.60)
C = c(75.45, 86.66, 103.36, 123.2, 107.53, 128.9, 128.49)
library(ggplot2)
library(tidyr)
library(dplyr)
data = data.frame(Location, A,B,C)
data %>% pivot_longer(.,-Location, names_to = "var",values_to = "val") %>%
filter(!is.na(val)) %>%
mutate(NewVar = var) %>%
add_row(., Location = c(1,1),
var = c("B","B"),
val = c(0,30),
NewVar = c("Out","Out")) %>%
ggplot(aes(x = Location, y = val, group = NewVar, color = NewVar))+
geom_point(size =2, colour = "black") +
ylab("")+
facet_wrap(.~var, strip.position = "left", ncol = 1, scales = "free_y", labeller = as_labeller(c(A= "A", B= "B", C= "C")))+
theme_bw() + theme(text = element_text(size=15), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), strip.background = element_blank(),
strip.placement = "outside")+
scale_x_continuous(breaks = 1:7) + theme(legend.position = "none") +
scale_shape_manual(values=c(1, 16, 21))
解决方案
这是@teunbrand 建议并应用了@Limey 的颜色校正
data %>% pivot_longer(.,-Location, names_to = "var",values_to = "val") %>%
filter(!is.na(val)) %>%
mutate(NewVar = var) %>%
add_row(., Location = c(1,1),
var = c("B","B"),
val = c(0,30),
NewVar = c("Out","Out")) %>%
ggplot(aes(x = Location, y = val, group = NewVar))+
geom_point(aes(Location, val, shape=var), size =2) +
ylab("")+
facet_wrap(.~var, strip.position = "left", ncol = 1, scales = "free_y", labeller = as_labeller(c(A= "A", B= "B", C= "C")))+
theme_bw() + theme(text = element_text(size=15), axis.text.x = element_text(colour = "black"), axis.text.y = element_text(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), strip.background = element_blank(),
strip.placement = "outside")+
scale_x_continuous(breaks = 1:7) + theme(legend.position = "none") +scale_shape_manual(values=c(19, 0, 15))
推荐阅读
- java - 风暴日志文件/文件目录:它在哪里?
- php - 服务器升级后 PHP 软件的问题(HTTP Header)
- rust - 在 crate 的 API 中发布具体类型而不是 impl trait 有什么好处?
- laravel - 添加新路由后Laravel错误500(未定义变量'e')?
- heroku - 如何使用本地路径依赖于 Heroku 的 phoenix 应用程序
- python - 如何使用python过滤CSV文件中两个日期之间的行并重定向到另一个文件?
- angular - 如何使用给定的注入令牌将依赖项注入提供程序?
- eclipse - 如何使 IntelliJ IDEA 在侧栏中显示不同出现的颜色
- ios - 运行时警告:无法加载从带有标识符“bundleId”的包中的笔尖引用的图像“imageName”
- javascript - JavaScript:将来自 youtube 频道的视频作为 API json 导入我的网站