prosource

ggplot2 축, 범례 등이 없는 그림

probook 2023. 7. 2. 20:50
반응형

ggplot2 축, 범례 등이 없는 그림

저는 바이오컨덕터의 헥스빈(내가 할 수 있는)을 사용하여 축도, 레이블도, 배경도, 누틴도 없는 전체(png) 디스플레이 영역을 채우는 플롯을 생성하고 싶습니다.

Chase의 답변에 있는 제 의견에 따르면, 당신은 이 많은 것들을 제거할 수 있습니다.element_blank:

dat <- data.frame(x=runif(10),y=runif(10))

p <- ggplot(dat, aes(x=x, y=y)) + 
        geom_point() +
        scale_x_continuous(expand=c(0,0)) + 
        scale_y_continuous(expand=c(0,0))   

p + theme(axis.line=element_blank(),axis.text.x=element_blank(),
          axis.text.y=element_blank(),axis.ticks=element_blank(),
          axis.title.x=element_blank(),
          axis.title.y=element_blank(),legend.position="none",
          panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
          panel.grid.minor=element_blank(),plot.background=element_blank())

이것을 저장할 때 결과 .png의 가장자리 주변에는 아직 약간의 여유가 있는 것처럼 보입니다.아마도 다른 누군가가 그 구성 요소를 제거하는 방법을 알고 있을 것입니다.

(기록 노트:ggplot2 버전 0.9.2 이후,opts더 이상 사용되지 않습니다.대신 사용theme()및 대체theme_blank()와 함께element_blank().)

Re: 테마 등으로 옵션 변경(나태한 사람의 경우):

theme(axis.line=element_blank(),
      axis.text.x=element_blank(),
      axis.text.y=element_blank(),
      axis.ticks=element_blank(),
      axis.title.x=element_blank(),
      axis.title.y=element_blank(),
      legend.position="none",
      panel.background=element_blank(),
      panel.border=element_blank(),
      panel.grid.major=element_blank(),
      panel.grid.minor=element_blank(),
      plot.background=element_blank())

현재 답변이 불완전하거나 비효율적입니다.다음은 (아마도) 결과를 달성하는 가장 빠른 방법입니다(사용).theme_void():

data(diamonds) # Data example
ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) +
      theme_void() + theme(legend.position="none")

결과는 다음과 같습니다.

enter image description here


만약 당신이 라벨을 없애는 것에 관심이 있다면,labs(x="", y="")요령을 터득합니다.

ggplot(data = diamonds, mapping = aes(x = clarity)) + geom_bar(aes(fill = cut)) + 
      labs(x="", y="")
'opts' is deprecated.

ggplot2 >= 0.9.2사용하다

p + theme(legend.position = "none") 

파티에 늦었지만, 관심이 있을지도...

다음의 조합을 찾습니다.labs그리고.guides많은 경우 유용한 사양:

그리드와 배경만 있으면 됩니다.

ggplot(diamonds, mapping = aes(x = clarity)) + 
  geom_bar(aes(fill = cut)) + 
  labs(x = NULL, y = NULL) + 
  guides(x = "none", y = "none")

enter image description here

한 축 또는 두 축의 눈금 표시 레이블만 표시하지 않을 경우:

ggplot(diamonds, mapping = aes(x = clarity)) + 
  geom_bar(aes(fill = cut)) + 
  guides(x = "none", y = "none")

enter image description here

xy <- data.frame(x=1:10, y=10:1)
plot <- ggplot(data = xy)+geom_point(aes(x = x, y = y))
plot
panel = grid.get("panel-3-3")

grid.newpage()
pushViewport(viewport(w=1, h=1, name="layout"))
pushViewport(viewport(w=1, h=1, name="panel-3-3"))
upViewport(1)
upViewport(1)
grid.draw(panel)

가스를 사용하는 것이 더 간단합니다.

library(ggeasy)
p + theme_classic()+easy_remove_axes() + easy_remove_legend()

이것이 당신이 원하는 것입니까?

 p <- ggplot(myData, aes(foo, bar)) + geom_whateverGeomYouWant(more = options) +
 p + scale_x_continuous(expand=c(0,0)) + 
 scale_y_continuous(expand=c(0,0)) +
 opts(legend.position = "none")

저는 여기서 이 해결책을 찾지 못했습니다.다음과 같이 소 그림 패키지를 사용하여 모든 소 그림을 제거합니다.

library(cowplot)

p + theme_nothing() +
theme(legend.position="none") +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)

다음과 같이 theme.void()를 사용하여 동일한 작업을 수행할 수 있습니다.

p + theme_void() +
theme(legend.position="none") +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
labs(x = NULL, y = NULL)

언급URL : https://stackoverflow.com/questions/6528180/ggplot2-plot-without-axes-legends-etc

반응형