使用ggplotly将多行绘制为列

时间:2018-03-15 11:24:29

标签: r ggplot2 ggplotly

我有以下数据

    dput(head(new_data))
structure(list(series = c("serie1", "serie2", "serie3", 
"serie4"), Chr1_Coverage = c(0.99593043561, 0.995148711122, 
0.996666194154, 1.00012127128), Chr2_Coverage = c(0.998909597935, 
0.999350808049, 0.999696737431, 0.999091916132), Chr3_Coverage = c(1.0016871729, 
1.00161108919, 0.997719609642, 0.999887319775), Chr4_Coverage = c(1.00238874787, 
1.00024296426, 1.0032143002, 1.00118558895), Chr5_Coverage = c(1.00361001984, 
1.00233184803, 1.00250793369, 1.00019989912), Chr6_Coverage = c(1.00145962318, 
1.00085036645, 0.999767433622, 1.00018523387), Chr7_Coverage = c(1.00089620637, 
1.00201715802, 1.00430458519, 1.00027257509), Chr8_Coverage = c(1.00130277775, 
1.00332841536, 1.0027493578, 0.998107829176), Chr9_Coverage = c(0.998473062701, 
0.999400379593, 1.00130178863, 0.9992796405), Chr10_Coverage = c(0.996508132358, 
0.999973856701, 1.00180072957, 1.00172163916), Chr11_Coverage = c(1.00044015107, 
0.998982489577, 1.00072330837, 0.998947935281), Chr12_Coverage = c(0.999707836898, 
0.996654676531, 0.995380321719, 1.00116773966), Chr13_Coverage = c(1.00199118466, 
0.99941499519, 0.999850500793, 0.999717689167), Chr14_Coverage = c(1.00133747054, 
1.00232593477, 1.00059139379, 1.00233368187), Chr15_Coverage = c(0.997036875653, 
1.0023727983, 1.00020943048, 1.00089130742), Chr16_Coverage = c(1.00527426537, 
1.00318861724, 1.0004269482, 1.00471256502), Chr17_Coverage = c(0.995530811404, 
0.995103514254, 0.995135851149, 0.99992196636), Chr18_Coverage = c(0.99893371568, 
1.00452723685, 1.00006262572, 1.00418478844), Chr19_Coverage = c(1.00510422346, 
1.00711968194, 1.00552123413, 1.00527171097), Chr20_Coverage = c(1.00113612137, 
1.00130658886, 0.999390191542, 1.00178637085), Chr21_Coverage = c(1.00368753618, 
1.00162782873, 1.00056883447, 0.999797571642), Chr22_Coverage = c(0.99677846234, 
1.00168287612, 0.997645576841, 0.999297594524), ChrX_Coverage = c(1.04015901555, 
0.934772492047, 0.98981339011, 0.999960536561), ChrY_Coverage = c(9.61374227868e-09, 
2.50609172398e-07, 8.30448295172e-08, 1.23741398572e-08)), .Names = c("series", 
"Chr1_Coverage", "Chr2_Coverage", "Chr3_Coverage", "Chr4_Coverage", 
"Chr5_Coverage", "Chr6_Coverage", "Chr7_Coverage", "Chr8_Coverage", 
"Chr9_Coverage", "Chr10_Coverage", "Chr11_Coverage", "Chr12_Coverage", 
"Chr13_Coverage", "Chr14_Coverage", "Chr15_Coverage", "Chr16_Coverage", 
"Chr17_Coverage", "Chr18_Coverage", "Chr19_Coverage", "Chr20_Coverage", 
"Chr21_Coverage", "Chr22_Coverage", "ChrX_Coverage", "ChrY_Coverage"
), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))

enter image description here

我想把它描绘成这个

enter image description here

我想过从第二列开始转置数据,并使用以下代码在初始数据的第一列中命名新的转置数据:

output$Plot_1 <- renderPlotly({
Plot_1_new_data[,2:24] <- lapply(Plot_1_new_data[,2:24], as.numeric)
# first remember the names
n <- as.data.frame(Plot_1_new_data[0:nrow(Plot_1_new_data),1])   
# transpose all but the first column (name)
Plot_1_new_data_T <- as.data.frame(t(Plot_1_new_data[,-1]))
colnames(Plot_1_new_data_T) <- n
#plot data
library(reshape)
melt_Transposed_Plot_1_new_data <- melt(Plot_1_new_data_T,id="series")
  ggplotly(melt_Transposed_Plot_1_new_data,aes(x=series,y=value,colour=variable,group=variable)) + geom_line()
})

然而,当我检查&#34; Plot_1_new_data_T&#34;似乎第一列被命名为c(&#34; serie1&#34;,&#34; serie2&#34;,...&#34; serie14&#34;)其余的被命名为NA。< / p>

任何想法如何继续,因为我对R和闪亮都是新手。

1 个答案:

答案 0 :(得分:0)

这样的东西?

xm = melt(x)
ggplot(xm[xm$variable != 'ChrY_Coverage' & xm$variable != 'ChrX_Coverage', ], 
       aes(as.integer(variable), value, color=series)) +
  geom_line() +
  scale_x_continuous(breaks = as.integer(xm$variable), 
                     labels = as.character(xm$variable)) +
  theme(axis.text.x = element_text( angle=45, hjust = 1))
ggplotly()

enter image description here

请注意,最后两列已从此绘图中删除,因为它们具有如此不同的比例,包括它们会掩盖其他列中的任何变化。如果要包含所有列,可以改为使用:

ggplot(xm, aes(as.integer(variable), value, color=series)) +
   geom_line() +
   ...