按组划分的ggplot直方图颜色不起作用
ggplot histogram colour by group not working
好的,我有一个这样的 df
a <- c(1,2,2,3)
b <- c(4,5,6,7)
ab <- data.frame(a, b)
ab
我用 ggplot 绘制了一个简单的直方图,并尝试按 x 变量上色。
ggplot(ab, aes(x=a, fill=a)) +geom_histogram()
输出图没有颜色。我显然在某处犯了一个小错误。
我认为使用条形几何图形可能效果更好。为了使填充起作用,您可以将 a
更改为一个因子。
a <- c(1,2,2,3)
b <- c(4,5,6,7)
ab <- data.frame(a, b)
ab
ggplot(ab, aes(x=a,
fill=as.factor(a))) +
geom_bar()
另一种方法,a
需要因子
library(ggplot2)
a <- c(1,2,2,3)
b <- c(4,5,6,7)
ab <- data.frame(a, b)
ab
ggplot(ab, aes(x=a, fill=as.factor(a))) +
geom_histogram()
好的,我有一个这样的 df
a <- c(1,2,2,3)
b <- c(4,5,6,7)
ab <- data.frame(a, b)
ab
我用 ggplot 绘制了一个简单的直方图,并尝试按 x 变量上色。
ggplot(ab, aes(x=a, fill=a)) +geom_histogram()
输出图没有颜色。我显然在某处犯了一个小错误。
我认为使用条形几何图形可能效果更好。为了使填充起作用,您可以将 a
更改为一个因子。
a <- c(1,2,2,3)
b <- c(4,5,6,7)
ab <- data.frame(a, b)
ab
ggplot(ab, aes(x=a,
fill=as.factor(a))) +
geom_bar()
另一种方法,a
需要因子
library(ggplot2)
a <- c(1,2,2,3)
b <- c(4,5,6,7)
ab <- data.frame(a, b)
ab
ggplot(ab, aes(x=a, fill=as.factor(a))) +
geom_histogram()