0

I am new to R and just preparing uni project. My code is very simple but couldn't find out where i am doing wrong.

 setwd("C:/abc")
heartFailure <- read.csv(file = 'example.csv')
df <- data.frame(heartFailure)
dfm <- melt(df[c('age','platelets')],id.vars = 1)
ggplot(dfm,aes(x=age, y=platelets)) + geom_bar()

I am getting following error and tried to find out on solution on google but couldn't get anything. Thanks in advance for any help.

Error in FUN(X[[i]], ...) : object 'platelets' not found
Phil
  • 7,287
  • 3
  • 36
  • 66
Shahroz javaid
  • 169
  • 1
  • 1
  • 12
  • Can you check the `names(dfm)` – akrun Dec 11 '20 at 17:34
  • yes i did. names are correct. – Shahroz javaid Dec 11 '20 at 17:36
  • 1
    Can you show dput of a small example i.e `dput(head(df[c('age', 'platelets')], 10))` – akrun Dec 11 '20 at 17:37
  • If `df` has two numeric columns called "age" and "platelets" and you do `melt(df[c('age','platelets')], id.vars = 1)` then `dfm` will be a data frame with three columns called "age", "variable" and "value". This is not what you want. If you want to plot age against platelets, just pass `df` instead of `dfm` to ggplot (and use `geom_col` instead of `geom_bar` ) – Allan Cameron Dec 11 '20 at 17:41
  • i want to plot two columns age and platelets but you are right its showing three columns exactly what you told. let me try your sol. – Shahroz javaid Dec 11 '20 at 17:46
  • @AllanCameron I tried your solution but i didn't work. its still showing dfm with three columns "age" , "variable" and "value". – Shahroz javaid Dec 11 '20 at 17:55
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Dec 11 '20 at 18:42
  • Try using `heartFailure` as the data frame in ggplot – Allan Cameron Dec 11 '20 at 19:07

0 Answers0