ggplot2를 사용하여 만든 그림의 배경색을 변경하는 방법
기본적으로 ggplot2는 배경이 회색인 그림을 생성합니다.플롯의 배경색을 변경하려면 어떻게 해야 합니까?
예를 들어, 다음 코드로 생성된 그림입니다.
library(ggplot2)
myplot<-ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + geom_line()
myplot
패널의 배경색을 변경하려면 다음 코드를 사용합니다.
myplot + theme(panel.background = element_rect(fill = 'green', colour = 'red'))
그림의 색을 변경하려면(패널의 색은 변경하지 않음) 다음 작업을 수행할 수 있습니다.
myplot + theme(plot.background = element_rect(fill = 'green', colour = 'red'))
테마에 대한 자세한 내용은 여기를 참조하십시오. 범례, 축 및 테마에 대한 빠른 참조 시트를 참조하십시오.
더 이상 사용되지 않도록 하려면opts
그리고.theme_rect
사용:
myplot + theme(panel.background = element_rect(fill='green', colour='red'))
테마_그레이를 기반으로 하지만 일부 변경 사항과 격자선 색상/크기 제어를 포함한 몇 가지 추가 사항을 포함하여 사용자 지정 테마를 정의하려면 다음을 수행합니다(ggplot2.org 에서 더 많은 옵션을 사용할 수 있습니다)
theme_jack <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.text = element_text(colour = "white"),
axis.title.x = element_text(colour = "pink", size=rel(3)),
axis.title.y = element_text(colour = "blue", angle=45),
panel.background = element_rect(fill="green"),
panel.grid.minor.y = element_line(size=3),
panel.grid.major = element_line(colour = "orange"),
plot.background = element_rect(fill="red")
)
}
나중에 ggplot을 마스킹하지 않고 호출할 때 사용자 정의 테마를 기본값으로 설정하려면 다음을 수행합니다.
theme_set(theme_jack())
현재 설정된 테마의 요소를 변경하려는 경우:
theme_update(plot.background = element_rect(fill="pink"), axis.title.x = element_text(colour = "red"))
현재 기본 테마를 개체로 저장하는 방법
theme_pink <- theme_get()
참고:theme_pink
는 목록인 반면에theme_jack
함수였습니다.테마를 theme_jack use로 되돌리려면theme_set(theme_jack())
반면에 테마로 돌아가기 위해_핑크 사용.theme_set(theme_pink)
.
대체할 수 있습니다.theme_gray
타고theme_bw
의 정의로theme_jack
원하신다면사용자 정의 테마가 닮기 위해theme_bw
그러나 모든 격자선(x, y, major 및 minor)이 꺼진 경우:
theme_nogrid <- function (base_size = 12, base_family = "") {
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(
panel.grid = element_blank()
)
}
마지막으로, 여기에 대한 논의를 기반으로 하지만 더 이상의 사용이 금지되지 않도록 업데이트된 Chorletths 또는 다른 매핑 gplot을 그릴 때 유용한 보다 급진적인 테마입니다.여기서의 목적은 지도에서 회색 배경과 주의를 분산시킬 수 있는 다른 특징을 제거하는 것입니다.
theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.2, "lines"),
legend.position="right",
legend.text=element_text(size=rel(0.8)),
legend.title=element_text(size=rel(0.8), face="bold", hjust=0),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.2)),
strip.background=element_rect(fill="grey90", colour="grey50"),
strip.text.x=element_text(size=rel(0.8)),
strip.text.y=element_text(size=rel(0.8), angle=-90)
)
}
여기 ggplot2 배경을 흰색으로 만드는 사용자 지정 테마와 출판물 및 포스터에 유용한 여러 가지 변경 사항이 있습니다.+me 테마를 선택하면 됩니다.+myme 뒤에 있는 +me로 옵션을 추가하거나 변경하려면 +myme의 옵션을 대체합니다.
library(ggplot2)
library(cowplot)
theme_set(theme_cowplot())
mytheme = list(
theme_classic()+
theme(panel.background = element_blank(),strip.background = element_rect(colour=NA, fill=NA),panel.border = element_rect(fill = NA, color = "black"),
legend.title = element_blank(),legend.position="bottom", strip.text = element_text(face="bold", size=9),
axis.text=element_text(face="bold"),axis.title = element_text(face="bold"),plot.title = element_text(face = "bold", hjust = 0.5,size=13))
)
ggplot(data=data.frame(a=c(1,2,3), b=c(2,3,4)), aes(x=a, y=b)) + mytheme + geom_line()
언급URL : https://stackoverflow.com/questions/6736378/how-do-i-change-the-background-color-of-a-plot-made-with-ggplot2
'programing' 카테고리의 다른 글
모델 및 관계 필드의 이름을 바꾸기 위한 장고 마이그레이션 전략 (0) | 2023.07.16 |
---|---|
장고 템플릿의 사전에서 사전을 통해 반복하는 방법은 무엇입니까? (0) | 2023.07.16 |
gitstash 실수: gitstash pop과 합병 충돌로 끝났습니다. (0) | 2023.07.16 |
R이 회귀 분석에서 지정된 요인 수준을 기준으로 사용하도록 강제하는 방법은 무엇입니까? (0) | 2023.07.16 |
시작 시 컨트롤러 매핑이 로깅되지 않음 (0) | 2023.07.16 |