0
votes

What the simplest way to make a bar plot of the following data frame:

df <- data.frame(val1=10, val2=50)

I want to plot the 10 and 50 on y-axis, and val1 and val2 on x-axis correspondingly. ggplot asks for aes but since there are no row names and I want to plot column names as x-values it gives an error.

Is there an easy way to do it without modifying the dataframe itself?

1
Had there been rownames you would have still been unable to draw barplot. You have to reshape your data first. There are numerous questions on SO showing how to do this. - AnilGoyal
Try this library(tidyverse); df <- data.frame(val1=10, val2=50); df %>% gather() %>% ggplot(aes(key, value))+ geom_col() - AntoniosK
See this or this for reference - AnilGoyal
plot(rev(stack(df))) - G. Grothendieck
@G.Grothendieck Thanks! This is most like what I was looking for. - danvoronov

1 Answers

1
votes

Perhaps converting df to a named vector and feeding it to barplot():

barplot(unlist(df)) 

enter image description here