首页 > 解决方案 > 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))

在此处输入图像描述

标签: rggplot2

解决方案


这是@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))

在此处输入图像描述


推荐阅读