“ Dotplot”可视化与因素

时间:2018-09-05 21:26:45

标签: r ggplot2

我不确定该如何处理。我想根据分类变量(因子)的数据框架在R中创建一个“点状”样式图,这样对于df的每一列,我都会绘制一列点,每个点都根据这些因素着色。例如,

my_df <- cbind(c('sheep','sheep','cow','cow','horse'),c('sheep','sheep','sheep','sheep',<NA>),c('sheep','cow','cow','cow','cow'))

然后我想最后得到一个3 x 5的点网格,每个点都根据绵羊/牛/马的颜色进行着色(嗯,由于NA而缺少了)。

1 个答案:

答案 0 :(得分:0)

您的意思是这样的吗?

my_df <- cbind(c('sheep','sheep','cow','cow','horse'),
               c('sheep','sheep','sheep','sheep',NA),
               c('sheep','cow','cow','cow','cow'))

df <- data.frame(my_df) # make it as data.frame
df$id <- row.names(df)  # add an id

library(reshape2)
melt_df <-melt(df,'id') # melt it

library(ggplot2)  # now the plot
  p <- ggplot(melt_df, aes(x = variable, fill = value))
  p + geom_dotplot(stackgroups = TRUE, binwidth = 0.3, binpositions = "all")

enter image description here