Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use columns as facets in graph

Tags:

r

csv

ggplot2

I have the CSV file sample below:

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

I want to graph it as a vertical bar in ggplot2 where the column Metric is the x-axis, and its value the y-axis; I want each Metric grouped by the columns named 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)

So far I used c(JASK,ADI,IYW,NMNLK,AGWF,SDGYU,SDFWF,FFSDD,SDSF) in the facet_grid() and ended up with an error:

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

while using facet_grid(~JASK+ADI+IYW+NMNLK+AGWF+SDGYU+SDFWF+FFSDD+SDSF) ended in a disaster (see here).

How can I achieve a graph where:

  • x-axis is Metric values
  • y-axis is the raw values of Metric
  • each are faceted/grouped by the variables JASK, ADI, IYW, NMNLK, AGWF, SDGYU, SDFWF, FFSDD, SDSF
like image 514
P A S T R Y Avatar asked Mar 28 '26 00:03

P A S T R Y


1 Answers

The key issue you're having is that ggplot expects data in long format. You can get your data into long format by using melt:

library(reshape2)
df.mlt <- melt(df, id.vars=c("Time", "Metric"))

You should examine df.mlt to see what I mean by "long format". With the data in long format plotting becomes trivial:

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))

I don't know what you wanted to do with the time variable, so I just used it for the fill of the bars. Note this is plotting only the data you excerpted.

enter image description here

like image 70
BrodieG Avatar answered Mar 31 '26 07:03

BrodieG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!