如何在ggplot2中更改Y轴。我正在使用此代码,但我收到此错误:
k <- read.table(text=" name1 Ereigniss distance
kamel kamel 1,251
kamel Dumper 2,750
kamel Graben 2,702
kamel Traktor 2.716
Dumper Kamel 2,750
Dumper Dumper 2,050
Dumper Graben 2,703
Dumper Traktor 2,570
Graben Kamel 2,702
Graben Dumper 2,703
Graben Graben 0,701
Graben Traktor 2,840
Traktor kamel 2,716
Traktor Dumper 2,570
Traktor Graben 2,840
Traktor Traktor 1,026
", header=T)
ggplot(k, aes(factor(name1), distance, fill = Ereigniss)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_brewer(palette = "Set1")+
labs(x="Ereignisse",y="Distanz")+
ylim(c(0,10))
然后我得到了这个错误:
Discrete value supplied to continuous scale
答案 0 :(得分:1)
有几个问题 1)您已删除数字中“距离”变量中的逗号。
2)为什么要将y轴限制在0到10之间?你所有的价值都是千万。我在您的代码中进行了这些更改并得到了这个很好的情节。
# remove commas (and correct possible typo on 2.716)
k$distance <- as.numeric(gsub("[,|.]", "", k$distance))
library(ggplot2)
ggplot(k, aes(factor(name1), distance, fill = Ereigniss)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_brewer(palette = "Set1")+
labs(x="Ereignisse",y="Distanz")