아래 설명 된대로 플롯에 사용 된 도형에 범례를 추가하는 데 도움이 필요합니다. 음모는 아래와 같습니다 - 상자 음모, 평균점, 신뢰 구간의 오차 막대.도형을 나타내는 범례 추가
결과 플롯은 다음과 같습니다 - 은 내가 어떻게 red circles
가 mean
과 green error bars
이 confidence interval
를 나타냅니다 말할하도록,이에 범례를 추가하는 방법은 무엇입니까?은 -
필수 범례
위를 생성하는 데 사용되는 데이터 및 코드를 아래 그림과 같이 참조에 대해서 설명한다.
df <- data.frame(cbind(mtcars[,1], mtcars[,2])) #mtcars[, 1:2]
colnames(df) <- c("metric", "group")
df$group <- factor(df$group)
p1 <- ggplot(data=df, aes(x=group, y=metric)) +
geom_boxplot()
metric_means <- aggregate(df$metric, list(df$group), mean)
metric_ci_95 <- aggregate(df$metric, list(df$group), function(x){1.96*sd(x)/sqrt(length(x))})
metric_mean_ci = data.frame(group=metric_means[,1],mean=metric_means[,2], ci=metric_ci_95[,2])
# plot mean
p1 <- p1 + geom_point(data=metric_means, aes(x=metric_means[,1], y=metric_means[,2]),
colour="red", shape=21, size=2)
#plot confidence interval
p1 <- p1 + geom_errorbar(data=metric_mean_ci, aes(ymin=mean-ci, ymax=mean+ci, x=group, y=mean),
color="green", width=.1)
p1
는 원과 오류 막대 모양을 표시하는 통계 요약을 표시 범례를 얻을 수 있도록 위의 코드에 추가 할 필요가 뭐?
'geom_point()'와'geom_errorbar()'함수에서'show.legend = TRUE'를 인자로 추가하면 어떻게 될까요? – Phil
@Phil - 당신이 제안한 것에 대한 전설이 보이지 않습니다. – user3206440
ggplot에서 범례를 얻으려면 값/변수를 'aes'의 미학으로 매핑해야합니다. 'aes '안에'color' 또는'shape' 매개 변수를 옮기면 무언가를 얻을 수 있지만, 원하는 것을 선택하지 않을 가능성이 있습니다. – alistaire