【发布时间】:2021-06-03 01:05:17
【问题描述】:
我很难在 R 中获得多线图。我是 R 新手,现有的建议都不适合我。
我想为每个 pheno 绘制一条不同的线,x 为 time+1,y 为 adj_sc。
我从 excel 电子表格 (marginal_plot_data) 中提取的数据的小示例:
| time | adj_sc | pheno |
|---|---|---|
| -1 | 0.94 | 1 |
| -1 | 1.39 | 2 |
| -1 | 1.35 | 3 |
| 1 | 0.84 | 1 |
| 1 | 1.58 | 2 |
| 1 | 1.46 | 3 |
这里是 dput(aa)
c(`intercept class1` = 5.52, `intercept class2` = 4.66,
`intercept class3` = 3.69, `intercept class4` = 2.79,
`time class1` = 0.03, `time class2` = 0.84,
`time class3` = 0.35, `time class4` = 0.66,
`time2 class1` = -0.57, `time2 class2` = -0.03,
`time2 class3` = -0.89, `time2 class4` = 0.08,
`RENAME` = 0.045,
nsex = 0.83, brace = 0.67, orace = 0.04,
stderr = 0.68)
下面是一些代码:
library(shiny)
library(readxl)
library(ggplot2)
library(plotly)
library(plyr)
library(dplyr)
marginal_plot_data<-read_xlsx( path="PATH_TO/PLOT_DATA.xlsx" )
load(file="PATH_TO/RENAME_FILE.Rda" )
aa <- rename( aa, c( "RENAME" = "NEW_NAME" ) )
ui <- fluidPage(
plotOutput("constPlot")
}
server <- function( input, output, session ) {
output$constPlot <- renderPlot({
sub_data1 <- as.data.frame( marginal_plot_data )
sub_data5 <- sub_data1 %>% group_by(pheno)
sub_data10 <- as.data.frame(sub_data5)
plo <- ggplot(data=sub_data10, aes(x=time, y=adj_sc, group=pheno)) +
geom_line() +
geom_point()
fig <- ggplot(plo)
fig
})
}
这是我得到的当前错误,但它是不断变化的哈哈:
UseMethod("rename") 中的错误: 没有适用于“重命名”的方法应用于“c('double', 'numeric')”类的对象
添加 plyr 时。在重命名函数之前我得到了同样的错误。
当删除 plyr 并尝试使用 dplyr 的重命名时,我得到了同样的错误。
在添加 dplyr 之前使用 plyr 重命名重命名没有问题,但我无法让图形代码工作。
【问题讨论】:
-
是
aa使用load()加载的某个数据框或其他对象吗?在我看来,您可以尝试将重命名为rename(aa,NEW_NAME=RENAME)。但是在基本 R 中还有其他重命名选项,例如colnames(aa) <- c("Col1_name","Col2_name",...) -
如果您创建一个小的可重现示例以及预期的输出,这将更容易提供帮助。阅读how to give a reproducible example。图片不是共享数据/代码的正确方式。
-
你能提供
dput(aa)吗? -
@Baroque aa 不是数据框,它是一个命名向量。在我添加 dplyr 之前它曾经可以工作。
-
@Waldi 我添加了 dput (: