0

I am trying to paste x in aes() ggplot. I am running into an issue where ggplot is not recognizing the df column. Example below:

df2 <- data.frame(`Sum of MAE` = c(0.030156758080105, 0.0600065426668421, 
0.0602272459239397, 0.0704327240953608, 0.09, 0.0900000000000001, 
0.104746328560384, 0.106063964745531, 0.108373386847075, 0.110086738825851
), Company = c("COCO", "APWC", "EDS", "FREE", "VLYWW", "IKAN", "SPU", "ELON", 
"WTSL", "MTSL"), check.names=F)
        

when I paste names(df2)[1] I don't get the column values. Note* I must use aes() and not aes_...etc Also, attempted as.name() but no luck.

ggplot(df2, aes(paste0(names(df2)[1]), Company, group=1)) + geom_line()

Any assistance is appreciated.

jack kelly
  • 320
  • 1
  • 8
  • See linked post, try: `mycol = "cyl"; ggplot(mtcars, aes(!!sym(mycol), mpg)) + geom_point()` – zx8754 Oct 18 '21 at 20:18

1 Answers1

-1

We can use .data if the intention is to plot based on column name or another object that stores the column name

library(ggplot2)
ggplot(df2, aes(.data[[names(df2)[1]]], Company, group=1)) +
         geom_line()

NOTE: The .data method is preferred over the !! + sym route

akrun
  • 874,273
  • 37
  • 540
  • 662