0

I want to plot using ggplot2 the distribution of 5 variables corresponding to a matrix's column names

a <- matrix(runif(1:25),ncol=5,nrow=1)
colnames(a) <- c("a","b","c","d","e")
rownames(a) <- c("count")

I tried:

ggplot(data=melt(a),aes(x=colnames(a),y=a[,1]))+ geom_point()

However, this gives a result as if all columns had the same y value

Note: i'm using the reshape package for the melt() function

1 Answer 1

1

All columns look like they have the same y-value because you are only specifying 1 number in the y= statement. You are saying y=a[,1] which if you type a[,1] into your command window you will find is 0.556 (the number that everything is appearing at). I think this is what you want:

library(reshape2)
library(ggplot2)

a_melt<- melt(a)
ggplot(data=a_melt,aes(x=unique(Var2),y=value))+ geom_point()

enter image description here

Note that I saved a new dataset called a_melt so that things were easier to reference. Also since the data was melted,it is cleaner if we define our x-values to be the Var2 column of a_meltrather than the columns of a.

Sign up to request clarification or add additional context in comments.

2 Comments

I get Error in unique(Var2) : object 'Var2' not found. From where do Var2 and value come from ?
The code works for me. Did you run it using the original data you posted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.