Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use index number as x in ggplot? [closed]

Tags:

r

ggplot2

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?

like image 812
CptanPanic Avatar asked Sep 14 '14 13:09

CptanPanic


1 Answers

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') 

geom_bar

like image 178
Paulo E. Cardoso Avatar answered Oct 04 '22 03:10

Paulo E. Cardoso