在ggplot2中指定点的形状

时间:2018-06-09 17:09:15

标签: r ggplot2

我有一个带有检测列的CSV,其结果为Y或N.我已经让我的脚本根据该列更改形状,但我需要指定Y是填充的圆,而N是空心圆圈。

library("ggplot2")
Report213 <- read.csv("FILE_NAME")
ggplot(data = Report213, aes(x = factor(Station_ID, level = c("NEB","NWB","LBC","WB","HR","FDP","FS","NR","PB")), y = Result,  Group = Detect,  colour = Station_ID,shape = Detect
)) + geom_point(aes(shape=Detect,size = 2)) +
facet_grid( . ~ Chemical ) +facet_wrap( ~ Chemical, scales= "free_y",ncol = 1) + theme(
panel.background = element_rect(fill = "white",
                            colour = "white",
                            size = 0.5, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                            colour = "gray"), 
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                            colour = "white"),
strip.background =element_rect(fill="#454545"),
strip.text = element_text(colour = 'white')
)

欣赏任何指示。

关闭Report213的输出输出:

structure(list(Station_ID = structure(c(4L, 4L, 4L, 4L, 4L, 9L, 
3L, 9L, 3L, 3L, 9L, 3L, 3L, 5L, 7L, 2L, 6L, 7L, 5L, 7L, 8L, 1L, 
5L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L, 
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 
7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L), .Label = c("FDP", 
"FS", "HR", "LBC", "NEB", "NR", "NWB", "PB", "WB"), class = "factor"), 
Chemical = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("4,4'-DDT", "CHLORDANE", "Total Aroclors", 
"Total PAHs", "Total PCB Congeners"), class = "factor"), 
Result = c(78.4176, 66.8307, 59.7295, 50.4102, 40.9341, 36.6868, 
34.6394, 26.7728, 23.192, 18.091, 15.47568, 14.539, 13.8006, 
4.489, 2.0159, 1.99509, 1.71768, 1.69251, 1.5165, 1.39725, 
1.27822, 1.22813, 0.89586, 507.7, 135, 684, 8911, 4946, 780, 
4920, 137.9, 559.5, 239.51, 902, 376, 655.4, 8299, 6500, 
889, 502.8, 361.1, 17440, 555.8, 953, 5691, 1790, 0.3, 1, 
14, 12, 20, 20, 21, 10, 14, 7.6, 7.3, 23, 7.7, 11, 1.5, 0.28, 
8.1, 5.4, 11, 0.31, 0.62, 20, 22, 4.2, 6.8, 3.9, 6.7, 4.6, 
6.4, 13, 51, 4.2, 50.8, 43.1, 41.9, 4.1, 4.4, 3.9, 4, 4.2, 
4.5, 2.3, 4.3, 13, 6.8, 35, 1.1, 0.62, 0.053, 1, 7.4, 23, 
3.7, 0.056, 2, 0.055, 0.054, 0.12, 0.053, 0.057, 0.13, 0.088, 
0.11, 0.058, 1.1, 21, 1.5, 4.7, 1.6), Detect = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("N", "Y"), class = "factor")), class = "data.frame", row.names = c(NA, 
-115L))

1 个答案:

答案 0 :(得分:2)

您可以使用scale_shape_manual

指定形状

P.S:同时使用facet_gridfacet_wrap

编辑:在2018年7月发布ggplot2 v3.0.0后,您可以使用text / string指定形状。例如。 scale_shape_manual(values = c("circle", "circle open"))。查看更多here

library(tidyverse)

Report213 <- Report213 %>% 
  mutate(Station_ID = factor(Station_ID, 
                             level = c("NEB","NWB","LBC","WB","HR","FDP","FS","NR","PB")))

ggplot(data = Report213, 
       aes(x = Station_ID, 
           y = Result)) + 
  geom_point(aes(color = Station_ID, shape = Detect), size = 2) +
  scale_shape_manual(values = c(19, 1)) +
  facet_wrap( ~ Chemical, scales = "free_y", ncol = 1) + 
  theme(
    panel.background = element_rect(fill = "white",
                                    colour = "white",
                                    size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 0.5, linetype = 'solid',
                                    colour = "gray"), 
    panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
                                    colour = "white"),
    strip.background =element_rect(fill = "#454545"),
    strip.text = element_text(colour = 'white')
  )

编辑2:添加string ~ integer形状表以供将来参考

  pch_table <- c(
    "square open"           = 0,
    "circle open"           = 1,
    "triangle open"         = 2,
    "plus"                  = 3,
    "cross"                 = 4,
    "diamond open"          = 5,
    "triangle down open"    = 6,
    "square cross"          = 7,
    "asterisk"              = 8,
    "diamond plus"          = 9,
    "circle plus"           = 10,
    "star"                  = 11,
    "square plus"           = 12,
    "circle cross"          = 13,
    "square triangle"       = 14,
    "triangle square"       = 14,
    "square"                = 15,
    "circle small"          = 16,
    "triangle"              = 17,
    "diamond"               = 18,
    "circle"                = 19,
    "bullet"                = 20,
    "circle filled"         = 21,
    "square filled"         = 22,
    "diamond filled"        = 23,
    "triangle filled"       = 24,
    "triangle down filled"  = 25
)

使用的数据

Report213 <- structure(list(Station_ID = structure(c(4L, 4L, 4L, 4L, 4L, 9L, 
    3L, 9L, 3L, 3L, 9L, 3L, 3L, 5L, 7L, 2L, 6L, 7L, 5L, 7L, 8L, 1L, 
    5L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
    6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 
    4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L, 1L, 
    2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 
    7L, 7L, 8L, 9L, 9L, 9L, 1L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
    4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 9L, 9L, 9L), .Label = c("FDP", 
    "FS", "HR", "LBC", "NEB", "NR", "NWB", "PB", "WB"), class = "factor"), 
    Chemical = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L), .Label = c("4,4'-DDT", "CHLORDANE", "Total Aroclors", 
    "Total PAHs", "Total PCB Congeners"), class = "factor"), 
    Result = c(78.4176, 66.8307, 59.7295, 50.4102, 40.9341, 36.6868, 
    34.6394, 26.7728, 23.192, 18.091, 15.47568, 14.539, 13.8006, 
    4.489, 2.0159, 1.99509, 1.71768, 1.69251, 1.5165, 1.39725, 
    1.27822, 1.22813, 0.89586, 507.7, 135, 684, 8911, 4946, 780, 
    4920, 137.9, 559.5, 239.51, 902, 376, 655.4, 8299, 6500, 
    889, 502.8, 361.1, 17440, 555.8, 953, 5691, 1790, 0.3, 1, 
    14, 12, 20, 20, 21, 10, 14, 7.6, 7.3, 23, 7.7, 11, 1.5, 0.28, 
    8.1, 5.4, 11, 0.31, 0.62, 20, 22, 4.2, 6.8, 3.9, 6.7, 4.6, 
    6.4, 13, 51, 4.2, 50.8, 43.1, 41.9, 4.1, 4.4, 3.9, 4, 4.2, 
    4.5, 2.3, 4.3, 13, 6.8, 35, 1.1, 0.62, 0.053, 1, 7.4, 23, 
    3.7, 0.056, 2, 0.055, 0.054, 0.12, 0.053, 0.057, 0.13, 0.088, 
    0.11, 0.058, 1.1, 21, 1.5, 4.7, 1.6), Detect = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 
    2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), 
    .Label = c("N", "Y"), class = "factor")), 
    class = "data.frame", row.names = c(NA, 
    -115L))

reprex package(v0.2.0)创建于2018-06-09。