ggplot2 code runs and updates plot but no data actually shows up

I'm trying to produce a graph using ggplot2 in R. While I am able to generate the graph I want using plot() and when I run the ggplot code below it shows up with the proper axes, but there's no data or scale.

Data looks something like this:

data <- data.frame(area=c("alpha", "alpha", "bravo", "bravo", "charlie", "charlie"),
                   year=c(2001, 2002, 2001, 2002, 2001, 2002),
                   rate=c(.94, .90, .83, .87, .87, .95))

where area is a character variable and year/rate are but numerical.

If I run

plot(data$year, data$rate)

I get in the plot window the graph that I expect to see. What I'm trying to do is recreate this in ggplot as a line graph. Here's what I tried:

gg <- ggplot(data=data, aes(x="year", y="rate", group="area"))
gg + geom_point()
gg + geom_line()
gg

# also tried subsetting to remove the group issue, thinking that might help but it didn't. also removed line from this too
temp <- data[data$area=="alpha",]
gg <- ggplot(data=temp, aes(x="year", y="rate"))
gg + geom_point()
gg

# also tried this which manages to put a dot in the middle of the still empty plot
ggplot(data=test) +
     geom_point(mapping=aes(x="Year", y="Attendance Rate", group="Area"))

In both instances, I get the same result: the code runs fine (error free) and the plot window refreshes to whichever one I treid most recently, but while it has proper X and Y labels (year/rate) it doesn't actually put the data on there. There also is no scale so it's not reading that information in either, apparently.

What am I doing wrong here? I've been using the guides and reference sheets below but I (at least would like to think I) am recreating them properly but clearly I'm not.

https://www.rstudio.com/wp-content/uploads/2016/11/ggplot2-cheatsheet-2.1.pdf

http://r-statistics.co/ggplot2-cheatsheet.html

http://www.sthda.com/english/wiki/ggplot2-line-plot-quick-start-guide-r-software-and-data-visualization

http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html


Don't use quotes.
With: aes("year", "rate") you were plotting words "year" and "rate" .
With aes(year, rate) you plot variable year and rate in data data .

ggplot(data, aes(year, rate, group = area)) + 
    geom_point() + 
    geom_line()

在这里输入图像描述

If for some reason you have to use quotes then use aes_string instead:

ggplot(data, aes_string("year", "rate", group = "area")) + 
    geom_point() + 
    geom_line()
链接地址: http://www.djcxy.com/p/30902.html

上一篇: 像ggplot中的群集与nmds?

下一篇: ggplot2代码运行并更新绘图,但实际上没有数据显示