ggplot2代码运行并更新绘图,但实际上没有数据显示 [英] ggplot2 code runs and updates plot but no data actually shows up
问题描述
我试图在R中使用 ggplot2
来生成一个图。虽然我能够使用 plot()
,当我运行下面的ggplot代码时,它显示了正确的坐标轴,但没有数据或缩放比例。
数据看起来像这样:
data < - data.frame(area = c(alpha,alpha,bravo,bravo ,charlie,charlie),
year = c(2001,2002,2002,2002,2002,2002),
rate = c(.94,.90,.83,。 87,.87,.95))
其中area是一个字符变量,year / rate是数值。
如果我运行
plot(data $ year,data $费率)
我在图表窗口中看到我期望看到的图表。我想要做的是在ggplot中重新创建一个线形图。这里是我的尝试:
gg < - ggplot(data = data,aes(x =year,y = ()),group =area))
gg + geom_point()
gg + geom_line()
gg
#也尝试使用子集来移除组问题,认为这可能有帮助,但事实并非如此。也删除了这一行
temp< - data [data $ area ==alpha,]
gg< - ggplot(data = temp,aes(x =year,y = rate))
gg + geom_point()
gg
#也尝试过这种方法,它可以在仍然为空的图的中间放置一个点
ggplot (data = test)+
geom_point(mapping = aes(x =Year,y =考勤率,group =Area))
在这两种情况下,我都得到了相同的结果:代码运行良好(无错误),并且绘图窗口刷新到最近我最近处理的任何一个,但是适当的X和Y标签(年/费率)它并不真正把数据放在那里。也没有比例尺,所以它显然不会读取这些信息。
我在这里做错了什么?我一直在使用下面的指南和参考手册,但我(至少想想我)正在重新创建它们,但显然我不是。
如果由于某些原因您必须使用引号,请使用 aes_string
相反:
ggplot(data,aes_string(year,rate,group =area))+
geom_point()+
geom_line()
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://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()
这篇关于ggplot2代码运行并更新绘图,但实际上没有数据显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!