5x1 행렬 geom_bar 패싯을 생성하고 막대의 음수 값이 ggplot에서 y 축 아래로 내려가도록 허용하는 방법은 무엇입니까?

Pdubois

다음 그림 :

여기에 이미지 설명 입력

다음 코드로 생성되었습니다.

library(ggplot2)
library(reshape2)

dat <- structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("High_expression", 
"KD.ip", "LG.ip", "LN.id", "LV.id", "LV.ip", "SP.id", "SP.ip"
), class = "factor"), ImmGen = structure(c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Bcells", 
"DendriticCells", "Macrophages", "Monocytes", "NKCells", "Neutrophils", 
"StemCells", "StromalCells", "abTcells", "gdTCells"), class = "factor"), 
    Exp_06hr = c(7174.40482999999, 23058.74882, 39819.39133, 
    15846.46146, 8075.78226, 105239.11609, 7606.34563, 19513.57747, 
    7116.51211, 6978.64995, 498.36828, 732.01788, 621.51576, 
    546.63461, 529.1711, 545.17219, 477.54658, 1170.50303, 550.99528, 
    607.56707, 775.0691, 1269.50773, 2138.69883, 1561.74652, 
    601.9372, 5515.59896, 744.48716, 997.32859, 639.13126, 657.64581, 
    4165.29899, 5465.1883, 7773.25723, 5544.86758, 3461.13442, 
    8780.64899, 4380.00437, 8721.84871, 3674.62723, 3911.00108, 
    2932.76554, 5903.48407, 6179.81046, 3683.64539, 2744.59622, 
    6760.37307, 4097.14665, 6845.31988, 2872.77771, 2912.84262
    ), Exp_24hr = c(1596.9091, 4242.52354, 9984.68861, 3519.18627, 
    1602.92511, 12203.57109, 1656.19357, 3389.93866, 1617.35484, 
    1579.00309, 715.47289, 643.98371, 689.40412, 580.26036, 608.22853, 
    695.10737, 830.77947, 670.34899, 640.67908, 637.47464, 356.75713, 
    393.13449, 549.60095, 466.76064, 336.95453, 617.20976, 339.2476, 
    469.57407, 292.86365, 305.45178, 2604.07605, 4210.64843, 
    5797.13123, 3650.88447, 2275.03269, 6475.27485, 2604.70614, 
    4796.3314, 2411.09694, 2458.23237, 1498.21516, 1996.6875, 
    2927.82836, 1911.00463, 1523.57171, 2199.62297, 1541.82034, 
    2815.82184, 1608.46099, 1588.80561), ExpDiff_06_24hr = c(5577.49572999999, 
    18816.22528, 29834.70272, 12327.27519, 6472.85715, 93035.545, 
    5950.15206, 16123.63881, 5499.15727, 5399.64686, -217.10461, 
    88.03417, -67.88836, -33.62575, -79.05743, -149.93518, -353.23289, 
    500.15404, -89.6838, -29.9075700000001, 418.31197, 876.37324, 
    1589.09788, 1094.98588, 264.98267, 4898.3892, 405.23956, 
    527.75452, 346.26761, 352.19403, 1561.22294, 1254.53987, 
    1976.126, 1893.98311, 1186.10173, 2305.37414, 1775.29823, 
    3925.51731, 1263.53029, 1452.76871, 1434.55038, 3906.79657, 
    3251.9821, 1772.64076, 1221.02451, 4560.7501, 2555.32631, 
    4029.49804, 1264.31672, 1324.03701)), .Names = c("Type", 
"ImmGen", "Exp_06hr", "Exp_24hr", "ExpDiff_06_24hr"), row.names = c(NA, 
-50L), class = "data.frame")

dat.m <- melt(dat)

 setwd("~/Desktop/")
 pdf("myfig.pdf",width=30,height=20)
 p <- ggplot(dat.m,aes(ImmGen,value)) + 
 geom_bar(aes(fill = variable),position = "dodge",stat="identity")+
 facet_wrap(~Type) 

 p
 dev.off();

위와 같이 (2x3) 행렬로 감싸지 않고 대신 (5x1) 행렬을 만들도록 수정하려면 어떻게해야합니까? 따라서 각 행은 y 축의 스케일을 갖습니다.

두 번째로 파란색 막대 (ExpDiff_06_24hr)에 음수 값이 포함될 수 있습니다. 플롯에서 막대가 y 축에서 0 아래로 내려가도록 어떻게 표시 할 수 있습니까?

Jaap

명확성을 위해 서브 플롯은 한 행이 아니라 한 열에 표시되어야한다고 생각합니다. 이 답변에 대한 도움 ( 링크에 대한 hrbrmstr 덕분에 ) 과이 질문에 대한 답변이 필요하다고 생각하기 때문에 여기에 해결책이 있습니다.

dat$rank <- rank(dat$ExpDiff_06_24hr)
dat.m <- melt(dat, id = c("Type","ImmGen","rank"))

dat.t <- transform(dat.m, TyIm = factor(paste0(Type, ImmGen)))
dat.t <- transform(dat.t, TyIm = reorder(TyIm, rank(rank)))

p <- ggplot(dat.t, aes(TyIm,value)) + 
  geom_bar(aes(fill = variable), position = "dodge", stat="identity")+
  facet_wrap(~Type, ncol=1, scales="free") +
  scale_x_discrete("ImmGen", breaks=dat.t$TyIm, labels=dat.t$ImmGen)
p

결과: 여기에 이미지 설명 입력

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관