작은 데이터 프레임이 있고 그룹 전체의 평균 수준을 보여주는 선이있는 막대 차트를 만들고 싶습니다.
데이터 프레임 :
cpcost = c("Coll.Punishment","Coll.Punishment", "Ind.Punishment",
"Ind.Punishment")
color = c(0,1,0,1)
mysum = c(0.3408240, 0.2935982, 0.3460490, 0.1267057)
genlevel = c(0.3111111,0.3111111, 0.2181818, 0.2181818)
temp = as.data.frame(cbind(cpcost,color,mysum,genlevel))
따라서 결과는 다음과 같습니다.
문제는 지금 내가 정말 못생긴 방식으로하고 있다는 것입니다 (아래 참조). 기본적으로 수평 오차 막대를 먼저 추가합니다. 하지만 각 막대의 오차 막대 사이에 작은 공간이 있기 때문에 :
또한 오차 막대 사이의 작은 공간을 채우기 위해 수평선을 그립니다. (유일한 수평 막대를 그리면 막대의 중간에서 시작됩니다).
나는 동일한 결과에 도달하는 훨씬 더 우아한 방법이 있다고 믿습니다. 어떻게해야합니까?
감사!
ggplot(temp, aes(group=cpcost),
y = genlevel ) +
geom_bar(stat = "identity",aes(x = factor(color),
y = mysum,
group=cpcost,
fill = factor(color))) +
geom_errorbar(aes(x=factor(color),
y=genlevel,
ymin=genlevel,
ymax=genlevel,
group=cpcost
)) +
geom_line( aes(x=factor(color), y=genlevel,group=cpcost)) +
labs(x = "Round", y = "Share of Emptying") +
scale_fill_discrete(name='Crime',
labels=c("No", "Yes" )) +
ggtitle('Whistleblowing')+
facet_grid(~cpcost)
이것은 당신에게 약간 다른 결과를 줄 것이지만 당신의 필요에 충분하기를 바랍니다. 더 깨끗하고 내장 된 기능을 사용합니다 geom_hline
.
library(ggplot2)
df <- data.frame(cpcost = c("Coll.Punishment", "Coll.Punishment", "Ind.Punishment", "Ind.Punishment"),
color = c(0,1,0,1),
mysum = c(0.3408240, 0.2935982, 0.3460490, 0.1267057),
genlevel = c(0.3111111,0.3111111, 0.2181818, 0.2181818))
ggplot(df) +
geom_col(aes(factor(color), mysum, fill = factor(color))) +
geom_hline(aes(yintercept = genlevel)) +
scale_fill_discrete(name='Crime', labels=c("No", "Yes" )) +
facet_wrap( ~ cpcost) +
labs(x = "Round", y = "Share of Emptying") +
ggtitle('Whistleblowing')
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다