1
votes

I'm trying to create a simple graph of Apple's forward PE ratio over the last 10 years. My data is

year forward PE
2021 27.40
2020 30.12
2019 17.27
2018 16.69
2017 14.24
2016 12.66
2015 11.04
2014 13.79
2013 10.81
2012 13.00
2011 11.57

File name is PE and my code is:

ggplot(PE, aes(x=year, y='forward PE')) +
    geom_point()

This is what I'm getting back:

graph

The y variable is showing up as a horizontal line. Is there anyway to fix this?

Any help much appreciated.

1

1 Answers

3
votes

We need to backquote the column there is a space in the column (instead of single or double quote)

ggplot(PE, aes(x=year, y=`forward PE`)) +
    geom_point() + 
    geom_hline(aes(yintercept = mean(.data[["forward PE"]][1:5])))

-outut enter image description here


Or if we want to pass strings, extract with .data

ggplot(PE, aes(x=year, y= .data[["forward PE"]])) +
     geom_point()

data

PE <- structure(list(year = 2021:2011, `forward PE` = c(27.4, 30.12, 
17.27, 16.69, 14.24, 12.66, 11.04, 13.79, 10.81, 13, 11.57)), row.names = c(NA, 
-11L), class = "data.frame")