6

How do I plot a vector of y-values in ggplot2?

 pValues_Both <- c(0.004079,0.4392,0.6882,0.02053,0.4849,0.4938,0.3379,0.8408,0.07067,0.6603,0.2547,0.8692,0.8946,0.0696,0.6206,0.9559,0.9119,0.5162,0.2469,0.1582)

I have tried the following:

pValues_Both.m <- melt(pValues_Both)

ggplot(pValues_Both.m, aes(y=value)) + geom_bar(stat="identity")

Error in exists(name, envir = env, mode = mode) : 
argument "env" is missing, with no default

3 Answers 3

5

geom_bar() needs also x values to make the barplot. One workaround would be to provide just sequence of numbers that are the same lenght as value.

ggplot(pValues_Both.m, aes(x=seq_along(value),y=value)) + 
    geom_bar(stat="identity")
Sign up to request clarification or add additional context in comments.

Comments

3

It's also a ggplot qplot 1-liner w/o having to pre-make a data frame (it does it for you), but the same basic principle as Didzis':

qplot(x=1:length(pValues_Both), y=pValues_Both, geom="bar", 
    stat="identity", xlab="", ylab="", main="pValues_Both")

enter image description here

2 Comments

Getting this message Error: stat_count() must not be used with a y aesthetic. In addition: Warning message: `stat` is deprecated
'Warning: qplot() was deprecated in ggplot2 3.4.0.'
0

qplot creates the dataframe for you if not supplied. The plot you want is called a col plot in ggplot, so:

pVals = c(.0041,.4392,.6882,.02053,.4849,.4938,.3379,.8408,.07067,.6603,.2547,.8692,.8946,.0696,.6206,.9559,.9119,.5162,.2469,.1582)

qplot(x = 1:20, y = pVals, geom = "col")

1 Comment

Warning: qplot() was deprecated in ggplot2 3.4.0.

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.