2
請參閱下面的代碼。我試圖在圖中引入間距,以便按名稱對正方形,三角形和圓形線進行分組,但我需要在正方形和三角形之間留出更多空間(所以三角形和圓形將是名稱中的另一個較小的「組」組)。有關如何做到這一點的任何建議?我想添加另一個「級別」,或者通過自定義躲避,但不知道如何做到這一點。ggplot2按組內組之間添加條形間距
## Data
names <- c("bio", "bio", "bio", "coal", "coal", "coal")
upper <- c(1.02, 1.08, 1.20, 1.03, 1.04, 1.05)
lower <- c(0.96, 1.02, 1.0, 1.01, 1.02, 1.03)
coef <- c(0.99, 1.05, 1.11, 1.02, 1.03, 1.04)
level <- c(1,2,3,1,2,3)
level2 <- c(1,2,3,4,5,6)
overall <- data.frame(names,upper,lower,coef,level,level2)
overall$level <- as.factor(overall$level)
overall$level2 <- as.factor(overall$level2)
## Graph
graph <- ggplot(overall, aes(x=reorder(names, level2), y=coef, ymin=lower, ymax=upper, shape=level)) + geom_pointrange(position=position_dodge(width=.7), size=1)
## Lines & Labels
graph <- graph + geom_hline(aes(yintercept=1), linetype='dashed') + ylab("HR") + xlab("")
graph <- graph + theme(panel.background = element_rect(fill='white', colour='black')) + theme(axis.text.x = element_text(angle=40, hjust=1, size=11, colour='black'), axis.text.y=element_text(size=11, colour='black'))
## Legend
graph <- graph + coord_flip()
graph <- graph + scale_colour_discrete(guide = FALSE) + theme(legend.text.align = 0) + theme(legend.title=element_blank())
graph + theme(legend.position="right")
感謝您的建議,但你可以澄清多一點?如果我只是補充說它不起作用。 – user2543095
與錯誤參數是不是數值或邏輯:返回NA – user2543095
感謝您的幫助,它的工作 – user2543095