This seems like it should be easy, but I can't figure it out. I have a dataframe where the row names are created automatically as just index numbers, like the sample below.
> df
employee salary startdate
1 John Doe 21000 2010-11-01
2 Peter Gynn 23400 2008-03-25
3 Jolie Hope 26800 2007-03-14
I would like to plot this out using the row names at the x axis, but since it doesn't have a name, I can't figure this out. Can someone point me in the right direction?
It is not all clear for me (what is mapped to y? ) but
df <- structure(list(employee = c("John Doe", "Peter Gynn", "Jolie Hope"), salary = c(21000L, 23400L, 26800L), startdate = c("2010-11-01", "2008-03-25", "2007-03-14")), .Names = c("employee", "salary", "startdate"), row.names = c(NA, -3L), class = "data.frame") # if row.names are strings df$idu <- row.names(df) # if row numbers are integers (most likely!) df$idu <- as.numeric(row.names(df)) df library(ggplot2) ggplot(aes(x=idu, y = salary), data = df) + geom_bar(stat = 'identity')
or use the suggestion from @MrFlick without the idu column
ggplot(aes(x = as.numeric(row.names(df)), y = salary), data = df) + geom_bar(stat = 'identity') + labs(x='ID')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With