在图表中使用列作为构面

时间:2014-02-08 23:46:50

标签: r csv ggplot2

我有以下CSV文件示例:

Time,Metric,JASK,ADI,IYW,NMNLK,AGWF,SDGYU,SDFWF,FFSDD,SDSF
2,HAUY,90,0,0,1,0.5,0.9,1,0,0.5
2,Z12IQ8,92.7605,0.758917751,0.40473,0.985702222,0.872985058,0.937119768,0.985702222,0.40473,0.872985058
2,9KAH,93.4582,0.804411769,0.45694,0.987653333,0.865519578,0.942423344,0.987653333,0.45694,0.865519578
2,P0IIDS,93.4284,0.757522078,0.50426,0.982064444,0.832763706,0.946890638,0.982064444,0.50426,0.832763654
2,SDGHG772,92.7298,0.740310258,0.42053,0.983605556,0.878374248,0.938563198,0.983605556,0.42053,0.878374248
2,RS,93.0309,0.816649558,0.39101,0.990231111,0.883847443,0.936039127,0.990231111,0.39101,0.883847443
2,IHSFH215,93.08156,0.775562283,0.435494,0.985851333,0.866698007,0.940207215,0.985851333,0.435494,0.866697996
4,HAUY,89.563,0,0,1,0.499887673,0.89563,1,0,0.499887673
4,Z12IQ8,92.6136,0.760067731,0.427134234,0.984285922,0.879761369,0.936484701,0.984285922,0.427134234,0.879761369

我想将其绘制为ggplot2中的垂直条,其中列Metric是x轴,其值是y轴;我希望每个Metric按名为JASK,... SDSF的列分组:

require(ggplot2)

sample <- read.csv("path/to/data.csv")

ggplot(sample, aes(Metric, JASK)) + 
  labs(
    x="Metrics",
    y="Value (%)"
  ) +
  geom_bar(aes(fill = Metric), position="dodge", stat="identity") + 
  facet_grid(~    require(ggplot2)

sample <- read.csv("C:/Users/Troy/Dropbox/misc/data.csv")

ggplot(sample, aes(Metric, JASK)) + 
  labs(
    x="Metrics",
    y="Value (%)"
  ) +
  geom_bar(aes(fill = Metric), position="dodge", stat="identity") + 
  facet_grid(~Metric)

到目前为止,我在c(JASK,ADI,IYW,NMNLK,AGWF,SDGYU,SDFWF,FFSDD,SDSF)中使用了facet_grid(),最后出现了错误:

Error in layout_base(data, cols, drop = drop) :
    At least one layer must contain all variables used for facetting

使用facet_grid(~JASK+ADI+IYW+NMNLK+AGWF+SDGYU+SDFWF+FFSDD+SDSF)在灾难中结束(see here)。

如何实现图表:

  • x轴是Metric
  • y轴是Metric
  • 的原始值
  • 每个都由变量JASKADIIYWNMNLKAGWFSDGYU,{{1}分面/分组}},SDFWFFFSDD

1 个答案:

答案 0 :(得分:2)

您遇到的关键问题是ggplot需要长格式的数据。您可以使用melt

将数据转换为长格式
library(reshape2)
df.mlt <- melt(df, id.vars=c("Time", "Metric"))

您应该检查df.mlt以查看“长格式”的含义。随着长格式数据绘图变得微不足道:

ggplot(df.mlt, aes(x=Metric, y=value, fill=as.factor(Time))) + 
  geom_bar(stat="identity") +
  facet_wrap(~ variable, scales="free_y") +
  theme(axis.text.x=element_text(angle=90))

我不知道你想对time变量做什么,所以我只是用它来填充条形码。请注意,这仅绘制您摘录的数据。

enter image description here