Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

r - How can I plot variable with different lengths=

Hello everyone I′m trying to developed a plot with the next information:

x=c('La', 'Ce','Pr','Nd','Pm','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu')

utr=as.data.frame(t(nubtr))

V1        V2          V3        V4           V5         V6
1   431.223629  97.87928  609.913793 126.69584          Inf 111.486486
2  2214.209591 572.86432 2963.988920 455.69106 1130.0366300 231.250000
3   514.574899        NA          NA        NA   13.6704731  52.262931
4   144.967177       Inf  331.081081 923.62345  187.9396985 612.188366
5           NA        NA          NA        NA           NA         NA
6    54.810127  17.94454  108.297414  24.02626          Inf  34.527027
7    62.522202  16.98492   83.656510  13.49593   46.5201465  11.187500
8   442.510121  56.52174          NA        NA   12.3980424  57.758621
9     2.932166        NA    7.635135  22.38011    5.7788945  23.268698
10   27.154472 113.55311          NA        NA   41.7391304 229.674797
11    4.810127        NA          NA        NA          Inf   7.094595
12   53.108348  13.56784          NA        NA   63.5531136  17.812500
13   14.574899        NA          NA        NA    0.7830343   4.310345
14    5.295405       Inf   12.905405  38.01066   14.2211055  78.670360
15    1.178862        NA    1.681250  11.65992    2.4223602  16.260163

but I have a different length of x and y, so my question is how can a realize this kind of plot? The type of plot that I need is a scatterplot

Thank you so much Cheers

question from:https://stackoverflow.com/questions/65892618/how-can-i-plot-variable-with-different-lengths

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a tidyverse option:

library(dplyr)
library(tidyr) # pivot_longer
library(ggplot2)
longdat <- tidyr::pivot_longer(utr, V1:V6, names_to="V", values_to="val") %>%
  mutate(val = if_else(!is.finite(val), NA_real_, val))
longdat
# # A tibble: 90 x 3
#    x     V        val
#    <fct> <chr>  <dbl>
#  1 La    V1     431. 
#  2 La    V2      97.9
#  3 La    V3     610. 
#  4 La    V4     127. 
#  5 La    V5      NA  
#  6 La    V6     111. 
#  7 Ce    V1    2214. 
#  8 Ce    V2     573. 
#  9 Ce    V3    2964. 
# 10 Ce    V4     456. 
# # ... with 80 more rows

ggplot(longdat, aes(x, val, color = V)) +
  geom_point(na.rm = TRUE)

ggplot2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...