user4462740
user4462740

Reputation:

How to take all the names of colums as y-axis and plot them in the same graph with ggplot?

given a data frame like that:

   V1  V2  V3  V4 ... V25
1  0.3 0.2 0.0 0.0    0.0
2  0.0 0.0 0.1 0.15   0.0
   ...

I'd like to take the row names as x-axis and column names(V1:V25)as y-axis and plot multiple area plots in the same graph.

So i tried:

ggplot(topic_count, aes(x= row.names(topic_count),y = colnames(topic_count),group = colnames(topic_count))) + geom_point()

But got Error: Aesthetics must be either length 1 or the same as the data (22): x, y error.

Do you know how to do it properly? Thanks.

Upvotes: 2

Views: 250

Answers (1)

mtoto
mtoto

Reputation: 24198

We need to melt your data.frame first, then we can use ggplot with facet.wrap() to generate multiple plots in one graph.

library(reshape2)
library(ggplot2)

df.melted <- melt(as.matrix(df))

ggplot(df.melted, aes(factor(Var1), value)) + 
  geom_point() +
  facet_wrap(~ Var2)

enter image description here

Data

df <- structure(list(V1 = c(0.3, 0), V2 = c(0.2, 0), V3 = c(0, 0.1), 
    V4 = c(0, 0.15)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c("1", "2"))

Upvotes: 3

Related Questions