IT박스

ggplot2 범례를 아래쪽 및 수평으로

itboxs 2020. 8. 22. 08:24
반응형

ggplot2 범례를 아래쪽 및 수평으로


ggplot2 범례를 플롯의 맨 아래로 이동하고 수평으로 돌리려면 어떻게해야합니까?

샘플 코드 :

library(reshape2) # for melt
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend())

원하는 (대략적인) 결과 : 여기에 이미지 설명 입력


범례의 위치를 ​​이동하려면 다음 코드를 사용하십시오.

library(reshape2) # for melt
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend()) +
    theme(legend.position="bottom")

원하는 결과를 얻을 수 있습니다. 하단의 범례


요청한 내용을 정확히 제공하지는 않지만 매우 가까운 두 가지 불완전한 옵션 (적어도 색상을 조합 할 것임)

library(reshape2); library(tidyverse)
df <- melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
p1 + scale_fill_continuous(guide = guide_legend()) +
 theme(legend.position="bottom", legend.direction="vertical")

p1 + scale_fill_continuous(guide = "colorbar") + theme(legend.position="bottom")

2019-02-28에 reprex 패키지 (v0.2.1)로 생성됨

참고 URL : https://stackoverflow.com/questions/10032513/ggplot2-legend-to-bottom-and-horizontal

반응형