如何根据其他变量按因子水平排列Y轴

时间:2018-07-30 11:47:02

标签: r ggplot2

以下面的1364656 000280169fc78d704a2d4569bfb6f42ea4a1d5ae 8203 2015-11-17 3483047 00061dc0bc2452304107ddc75e7ee2908c729905 28618 2017-08-17 示例

data.frame

这是我的原始数据的简短示例。

在我的data.frame中,我有180个名称,这些名称与library(ggplot2) names <- c("AA", "BB", "CC", "DD", "EE", "FF") coding <- c("yellow", "blue", "red", "yellow", "blue", "blue") variable <- c("A1", "A2", "A3", "A4", "A5", "A6") values <- c(1,6,12,4,5,7) data_test <- data.frame(names,coding,variable,values) ggplot(data_test, aes(variable, reorder(names, -values), size = values, colour = coding)) + geom_point() 的8个级别相关联。 this is where I am, but I need the same color which represent the variable levels to be next to each other 这是我的位置,但是我需要相同的颜色(代表variable级别)彼此相邻

1 个答案:

答案 0 :(得分:1)

color中使用aes时: enter image description here

使用scale_manual_colour在编码列中为颜色名称赋值:

names <- c("AA", "BB", "CC", "DD", "EE", "FF")
coding <- c("yellow", "blue", "red", "yellow", "blue", "blue")
variable <- c("A1", "A2", "A3", "A4", "A5", "A6")
values <- c(1,6,12,4,5,7)

data_test <- data.frame(names,coding,variable,values)

ggplot(data_test, 
       aes(variable, reorder(names, -values),size = values,color=coding)) + 
  geom_point()+
  scale_colour_manual(values = c('yellow'='yellow','blue'='blue','red'='red'))

enter image description here