【发布时间】:2016-03-31 15:36:06
【问题描述】:
我正在将 csv 文件上传到 via shiny 并尝试从选定的列中绘制 ggplot。
output$plot = renderPlot(
{
df <- data_set()
gp <- NULL
if (!is.null(df)){
xv <- input$xaxisGrp
yv <- input$yaxisGrp
if (!is.null(xv) & !is.null(yv)){
if (sum(xv %in% names(df))>0){ # supress error when changing files
mdf <- melt(df,id.vars=xv,measure.vars=yv)
gp <- ggplot(data=mdf) +
geom_point(aes_string(x=xv,y="value",color="variable"))+
geom_smooth(method="lm")+
theme(axis.text.x=element_text(angle=45, hjust=1))+
theme_hc() +
scale_colour_hc()+theme(legend.title=element_blank())
}
}
}
return(gp)
}
我可以创建图表,但是当我尝试添加时
+geom_smooth(method="lm")
我对 lm 线没有任何想法可能会发生什么?
给定这样的数据集:
dput(df)
structure(list(load = c(1L, 18L, 36L, 72L, 108L, 144L, 216L),
throughput = c(64.9, 995.9, 1652.4, 1853.2, 1828.9, 1775,
1702.2)), .Names = c("load", "throughput"), class = "data.frame", row.names = c(NA,
-7L))
我尝试过:
plot(xy~yv, data=df)
我什么都没看到。但是为了测试它,当我执行以下操作时,它可以工作。我无法找出问题所在。同样,我正在将文件上传到闪亮的应用程序以绘制和创建模型。有什么想法吗?
plot(mtcars$mpg~mtcars$cyl) ##this works
【问题讨论】:
-
如果您使用
aes_string,您不是更愿意使用x="xv"吗?只是为了确认其他事情,我知道method="auto"没有区别,情节没有显示?最后一件事,如果两个 geoms 使用相同的aes,不应该在最初的ggplot调用中提供aes_string吗? -
我会尝试:
ggplot(data=mdf, aes_string(x="xv",y="value",color="variable")) + geom_point() + geom_smooth(method="auto"),只是为了检查它是否有所作为 -
@Konrad,我试过了,没区别,无法让 geom_smooth 正常工作
-
第二次修改aes怎么样?
-
请在
ggplot行之前插入message(str(mdf)),然后查看控制台上打印的内容。