Programing

색상 및 모양에 대한 범례를 하나의 범례로 결합

lottogame 2020. 11. 25. 07:28
반응형

색상 및 모양에 대한 범례를 하나의 범례로 결합


2 x 2 연구 설계에서 ggplot에 플롯을 만들고 있으며 2 가지 색상과 2 개의 기호를 사용하여 4 가지 다른 치료 조합을 분류하고 싶습니다. 현재 2 개의 범례가 있는데, 하나는 색상에 대한 것이고 다른 하나는 두 가지 모양에 대한 것입니다. 하나의 파란색 원, 하나의 빨간색 원, 하나의 파란색 삼각형, 하나의 읽기 삼각형을 가지도록 하나의 범례로 결합하려면 어떻게해야합니까?

일부 데이터 :

state1 <- c(rep(c(rep("N", 7), rep("Y", 7)), 2))
year <- rep(c(2003:2009), 4)
group1 <- c(rep("C", 14), rep("E", 14))
group2 <- paste(state1, group1, sep = "")
beta <- c(0.16,0.15,0.08,0.08,0.18,0.48,0.14,0.19,0.00,0.00,0.04,0.08,0.27,0.03,0.11,0.12,0.09,0.09,0.10,0.19,0.16,0.00,0.11,0.07,0.08,0.09,0.19,0.10)
    lcl <- c(0.13,0.12,0.05,0.05,0.12,0.35,0.06,0.13,0.00,0.00,0.01,0.04,0.20,0.00,0.09,0.09,0.06,0.06,0.07,0.15,0.11,0.00,0.07,0.03,0.05,0.06,0.15,0.06)
    ucl <- c(0.20,0.20,0.13,0.14,0.27,0.61,0.28,0.27,0.00,1.00,0.16,0.16,0.36,0.82,0.14,0.15,0.13,0.13,0.15,0.23,0.21,0.00,0.15,0.14,0.12,0.12,0.23,0.16)
data <- data.frame(state1,year,group1,group2,beta,lcl,ucl)

음모:

library(ggplot2)
pd <- position_dodge(.65)
ggplot(data = data, aes(x = year, y = beta, colour = state1, group = group2, shape = group1)) +
  geom_point(position = pd, size = 4) +
  geom_errorbar(aes(ymin = lcl, ymax = ucl),colour = "black", width = 0.5, position = pd) +
  scale_colour_hue(name = "Treatment & State",  #Legend label, use darker colors
                   labels = c("Control", "Exclosure"),
                   l = 40) +
  scale_shape(name = "State", labels = c("Non-F", "Flwr"))

여기에 이미지 설명 입력


당신은 동일한 사용해야 name하고 labels모양과 색상 규모 모두에 대한 값을.

pd <- position_dodge(.65)
ggplot(data = data,aes(x= year, y = beta, colour = group2, shape = group2)) +    
  geom_point(position = pd, size = 4) +
  geom_errorbar(aes(ymin = lcl, ymax = ucl), colour = "black", width = 0.5, position = pd) +
  scale_colour_manual(name = "Treatment & State",
                      labels = c("Control, Non-F", "Control, Flwr", "Exclosure, Non-F", "Exclosure, Flwr"),
                      values = c("blue", "red", "blue", "red")) +   
  scale_shape_manual(name = "Treatment & State",
                     labels = c("Control, Non-F", "Control, Flwr", "Exclosure, Non-F", "Exclosure, Flwr"),
                     values = c(19, 19, 17, 17))

여기에 이미지 설명 입력


빨간색 원, 파란색 원, 빨간색 삼각형, 파란색 삼각형 :

ggplot(data =data,aes(x= year, y = beta, shape=group2, colour=group2,group = group2)) +
geom_errorbar(aes(ymin = lcl, ymax = ucl),colour = "black", width = 0.5, position = pd) + 
geom_point(position = pd, size = 4) +  
scale_colour_manual(values=c("red","blue","red","blue")) +   
scale_shape_manual(values=c(19,19,17,17)) +
scale_fill_hue(name="Treatment & State",  #Legend label, use darker colors
labels=c("Control", "Exclosure"),l=40)

여기에 이미지 설명 입력

참고 URL : https://stackoverflow.com/questions/12410908/combine-legends-for-color-and-shape-into-a-single-legend

반응형