我正在嘗試繪制以下資料:
library(ggplot2)
my_data <- data.frame( a = abs(rnorm(1000,17,10)),
b = a)
my_data$col = as.factor(ifelse(my_data$a > 17, "red", "blue"))
ggplot(my_data, aes(x=a))
geom_histogram(binwidth=1)

但出于某種原因,當我嘗試添加顏色時,出現以下錯誤:
ggplot(my_data, aes(x=a))
geom_histogram(binwidth=1, color = my_data$col)
Error: Aesthetics must be either length 1 or the same as the data (59): colour
有人可以告訴我如何解決這個錯誤嗎?
謝謝
uj5u.com熱心網友回復:
您可以cut與 scale_fill_manual.
library(ggplot2)
my_data <- data.frame(a = abs(rnorm(1000,17,10)))
my_data$col = cut(my_data$a, c(-Inf, 17, Inf))
ggplot(my_data, aes(x=a, fill = col))
geom_histogram(binwidth=1)
scale_fill_manual(breaks = levels(my_data$col), values = c('blue', 'red'))

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315880.html
上一篇:R中的圖形不改變顏色
