我最终的目标是绘制颜色与高度相对应的数据点(原始数据是.csv文件中的列)。我用高度列创建了一个数据框,现在我正在尝试创建一个新的列,在高度增加时分配颜色。范围是0-400,所以我想每50英尺改变颜色。
e_miss_df$color <- ""
for (i in nrow(e_miss_df$e_miss.altitude.feet.)) {
if (i <= 50.0){
n <- "#00A600FF"
e_miss_df$color[i] <- n
} else if (i <= 100.0) {
n <- "#3EBB00FF"
e_miss_df$color[i] <- n
} else if (i <= 150.0) {
n <- "#8BD000FF"
e_miss_df$color[i] <- n
} else if (i <= 200.0){
n <- "#E6E600FF"
e_miss_df$color[i] <- n
} else if (i <= 250.0) {
n <- "#E8C32EFF"
e_miss_df$color[i] <- n
} else if (i <= 300.0) {
n <- "#EBB25EFF"
e_miss_df$color[i] <- n
} else if (i <= 350.0) {
n <- "#EDB48EFF"
e_miss_df$color[i] <- n
} else if (i <= 400.0) {
n <- "#F0C9C0FF"
e_miss_df$color[i] <- n
} else {
n <- "black"
e_miss_df$color[i] <- n
}
}
我没有收到任何错误,但是列没有被填充(除了第6行?)。我知道它现在非常低效,但我还是很新的......任何想法/建议都将不胜感激!如果你有更好的方法来获得情节,那就太棒了哈哈
答案 0 :(得分:0)
在R中,您通常希望处理整个数据框并避免循环。你可以尝试这样的事情:
#function that will assign color based on values
definecolor <- function(x) {
color = "black"
if (x <= 50) color <- "#00A600FF"
if (x <= 100) color <- "#3EBB00FF"
if (x <= 150) color <- "#8BD000FF"
if (x <= 200) color <- "#E6E600FF"
if (x <= 300) color <- "#EBB25EFF"
if (x <= 350) color <- "#EDB48EFF"
if (x <= 400) color <- "#F0C9C0FF"
return(color)
}
#apply that function to all values in a column and put those values in the new column
e_miss_df$color <- lapply(definecolor, e_miss_df$e_miss.altitude.feet.)
# you can also use ggplot to get intervals directly. something like this:
df <- data.frame(x = c(50,100,150,200,300,350,400), y=x)
ggplot(df, aes(x=x,y=x, color=cut_interval(y,6))) + geom_point()