我想用这个图:
dtf1 <- data.frame(ID = c(1:10),text = c("my", "and","keep","any","somenone",
"exist","tr",
"ggplot2","euf","edff"),
Diff = c(-5:4), stringsAsFactors = FALSE)
我尝试了这个: f(...,self = self)中的错误:中断和标签的长度不同 其中将在每个相应的栏中包含text列的文本作为标签。
dtf1$colour <- ifelse(dtf1$Diff < 0, "firebrick1","steelblue")
dtf1$hjust <- ifelse(dtf1$Diff > 0, 1.3, -0.3)
dtf1$colour <- ifelse(dtf1$Diff < 0, "negative","positive")
ggplot(dtf1,aes(ID,Diff,label="",hjust=hjust))+
geom_bar(stat="identity",position="identity",aes(fill = colour))+
scale_fill_manual(values=c(positive="firebrick1",
negative="steelblue")) +
scale_y_continuous(labels = dtf1$text) +
coord_flip()
错误是
:
f(...,self = self)中的错误:中断和标签的长度不同
答案 0 :(得分:1)
就像其中一项建议一样:
library(ggplot2)
dtf1 <- data.frame(ID = c(1:10),text = c("my", "and","keep","any","somenone",
"exist","tr",
"ggplot2","euf","edff"),
Diff = c(-5:4), stringsAsFactors = FALSE)
dtf1$colour <- ifelse(dtf1$Diff < 0, "firebrick1","steelblue")
dtf1$hjust <- ifelse(dtf1$Diff > 0, 1.3, -0.3)
dtf1$colour <- ifelse(dtf1$Diff < 0, "negative","positive")
ggplot(dtf1,aes(ID,Diff,label="",hjust=hjust))+
geom_bar(stat="identity",position="identity",aes(fill = colour))+
scale_fill_manual(values=c(positive="firebrick1",
negative="steelblue")) +
scale_y_continuous() +
geom_text(aes(label=text)) +
coord_flip()
由reprex package(v0.2.1)于2019-02-18创建
答案 1 :(得分:1)
与上面的基本相同,只是如果您愿意,我们使用geom_label
。
ggplot(dtf1,aes(ID,Diff,label="",hjust=hjust))+
geom_bar(stat="identity",position="identity",aes(fill = colour))+
scale_fill_manual(values=c(positive="firebrick1",
negative="steelblue"))+
geom_label(aes(label=text))+
coord_flip()