하나의 선 그래프 아래에 그룹화 된 막대를 어떻게 그릴 수 있습니까?동일한 차트에서 R-ggplot을 사용하여 막대와 두 개의 Y 축에 한 줄을 그리는 방법은 무엇입니까?
그림은 분류 실험 (예 : 정확도)의 실적을 선 (두껍고 표준)으로 표시 할 수 있습니다. 왼쪽 Y- 스케일을 사용하여 0 < Accuracy < 1
사이의 변이와 "This is accuracy"텍스트를 사용하십시오.
그런 다음 (예 : 텍스트 분류의) 피쳐 수를 막대로 표시 할 수 있습니다. 오른쪽 Y- 축, 0 < NOoFeatures < max(featuresX)
사이의 변형, 텍스트 : "기능 수". 엑스 스케일, 텍스트 "각 실험에 사용 된 기능".
실제로는 스택 형 (좋을 것) 또는 그룹화 (선호하는) 표시가 가능한 텍스트 기능의 네 가지 범주가 있습니다.)
## Mock-up data:
performanceExps <- c(0.4, 0.5, 0.65, 0.9) # Accuracy
FeaturesExp1 <- c(featuresA=1000, featuresB=0, featuresC=0, featuresD=0) # Used features Experiment 1
FeaturesExp2 <- c(featuresA=1000, featuresB=5000, featuresC=0, featuresD=0) # Used features Experiment 2
FeaturesExp3 <- c(featuresA=1000, featuresB=5000, featuresC=10000, featuresD=0) # Used features Experiment 3
FeaturesExp4 <- c(featuresA=1000, featuresB=5000, featuresC=10000, featuresD=20000) # Used features Experiment 4
Kohske 이벤트 (아래) 꽤 비슷하지만, 나는 내 문제 (사용 바)에 적응 할 수없는 하나의 예; 이제 모든 그레이 스케일 톤에 표시되는 경우, 완벽 할 것입니다.
library(ggplot2)
library(gtable)
library(grid)
grid.newpage()
# two plots
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") + theme_bw()
p2 <- ggplot(mtcars, aes(mpg, drat)) + geom_line(colour = "red") + theme_bw() %+replace%
theme(panel.background = element_rect(fill = NA))
# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))
# overlap the panel of 2nd plot on that of 1st plot
pp <- c(subset(g1$layout, name == "panel", se = t:r))
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t,
pp$l, pp$b, pp$l)
# axis tweaks
ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
grid.draw(g)
여기에 질문이 종료 -이 hrbmstr의 코드
featPerf <- data.frame(expS=c("1", "2", "3", "4"),
Experiment1=c(1000, 0, 0, 0),
Experiment2=c(1000, 5000, 0, 0),
Experiment3=c(1000, 5000, 10000, 0),
Experiment4=c(1000, 5000, 10000,20000),
accuracy=c(0.4, 0.5, 0.65, 0.9))
# make room for both axes ; adjust as necessary
par(mar=c(5, 12, 6, 7) + 0.4)
# plot the bars first with no annotations and specify limits for y
#barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", ylim=c(0, max(colSums(featPerf[2:5]))))
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)
# make the bounding box (or not...it might not make sense for your plot)
#box()
# now make the left axis
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)
# start a new plot
par(new=TRUE)
# plot the line; adjust lwd as necessary
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="", axes=FALSE, type="l", ylim=c(0,1), lwd=5)
# annotate the second axis
axis(4, ylim=c(0,1), col="black", col.axis="black", las=1)
#axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy", at = .5, side=3)
#title("An Example of Creative Axes", xlab="X values", ylab="Y=X")
mtext("Accuracy", side=4, line=3, cex.lab=1,las=2, col="black")
mtext("No. of features ", side=2, line=3, cex.lab=1,las=2, col="black")
귀하의 데이터는 무엇입니까? 원하는 RPM 샘플을 붙여 넣기 외에 어떤 방법으로 시도 했습니까? – hrbrmstr
@hrbrmstr 감사합니다 :) 위의 설명에 실물 크기의 데이터를 추가했습니다. – alex