반응형
R : 다중 레이어 ggplot에 대한 사용자 정의 범례
두 개의 개별 데이터 프레임에서 오는 데이터로 ggplot에 대한 사용자 정의 범례를 얻으려고합니다. 재현 가능한 최소 예는 아래를 참조하세요.
내가 달성하려는 것은 리본 채우기, 검은 색 선 및 빨간색 선을 설명하는 범례를 갖는 것입니다.
require(ggplot2)
x=seq(1,10,length=100)
data=data.frame(x,dnorm(x,mean=6.5,sd=1))
names(data)=c('x','new.data')
x.ribbon=seq(1,10,length=20)
ribbon=data.frame(x.ribbon,
dnorm(x.ribbon,mean=5,sd=1)+.01,
dnorm(x.ribbon,mean=5,sd=1)-.01,
dnorm(x.ribbon,mean=5,sd=1))
names(ribbon)=c('x.ribbon','max','min','avg')
ggplot()+geom_ribbon(data=ribbon,aes(ymin=min,ymax=max,x=x.ribbon),fill='lightgreen')+
geom_line(data=ribbon,aes(x=x.ribbon,y=avg),color='black')+
geom_line(data=data,aes(x=x,y=new.data),color='red')+
xlab('x')+ylab('density')
colour
및 을 설정하는 대신 지오메트리 미학 을 사용 하여fill
매핑 한 다음 또는 을 사용하십시오 . aes
scale_xxx_manual
scale_xxx_identity
예
ggplot()+geom_ribbon(data=ribbon,aes(ymin=min,ymax=max,x=x.ribbon,fill='lightgreen'))+
geom_line(data=ribbon,aes(x=x.ribbon,y=avg,color='black'))+
geom_line(data=data,aes(x=x,y=new.data,color='red'))+
xlab('x')+ylab('density') +
scale_fill_identity(name = 'the fill', guide = 'legend',labels = c('m1')) +
scale_colour_manual(name = 'the colour',
values =c('black'='black','red'='red'), labels = c('c2','c1'))
범례를 생성 guide = 'legend'
하려면 강제 scale_..._identity
로 지정해야합니다 .
scale_...manual
값에 대해 명명 된 벡터를 전달할 수 있습니다. 이름은 호출 내에서 색상을 호출 한 이름이어야하며 geom_...
멋지게 레이블을 지정할 수 있습니다.
참조 URL : https://stackoverflow.com/questions/18394391/r-custom-legend-for-multiple-layer-ggplot
반응형
'programing' 카테고리의 다른 글
치명적 : 빨리 감기 불가능, 중단 (0) | 2021.01.15 |
---|---|
문자열에서 정수로 @Value 주석 유형 캐스팅 (0) | 2021.01.15 |
명령 줄을 통한 이미지 압축 도구 (0) | 2021.01.15 |
SQL Server Compact는 Visual Studio 2013에서 중단됩니까? (0) | 2021.01.15 |
스 와이프하여 셀을 삭제하면 tableViewHeader가 셀과 함께 이동합니다. (0) | 2021.01.15 |