2017-01-27 7 views
1

これは新しくRに新しく、これが可能かどうかを知りたがっています。私は以下のデータセットを持っていて、同じ行にxyをプロットしたいので、xは19で終了し、21でピックアップします。ggplot2です。R:2つのデータセットを1行にプロットする

もし私がa、b、ectのような列をもっと持っていたら、Rはこれを扱うことができますか?あなたが削除した後、(ggplot2が長いデータを好きな)パッケージtidyrと機能gatherを使用してデータを再構築する必要があると思い

enter image description here

レッドドット= x グリーンドット= y

mydata = structure(list(q = 1:7, x = c(12L, 18L, 21L, 19L, 0L, 0L, 0L), 
    y = c(0L, 0L, 0L, 0L, 21L, 25L, 23L)), .Names = c("q", "x", 
"y"), class = "data.frame", row.names = c(NA, -7L)) 
+0

電位D宝くじhttp://stackoverflow.com/questions/37034285/graphing-3-axis-accelerometer-data-in-r/37035280#37035280 –

+0

@Lloydクリスマス。私はリンクが同じことを求めていたと信じています。私は検索しますが、あなたがリンクした特定の質問を見つけることができませんでした。私はそれを間違って検索していた可能性があります。ありがとうございました。 – user1901959

答えて

1

base Rプロットでこれを試してみてください:

df <- read.table(text='q x y 
       1 12 0 
       2 18 0 
       3 21 0 
       4 19 0 
       5 0 21 
       6 0 25 
       7 0 23 ', header=TRUE) 

df$y[df$y==0] <- df$x[df$x!=0] 
plot(df$q, df$y, pch=19, col=ifelse(df$x==0, 'green', 'red'), xlab='q', ylab='x or y') 
lines(df$q, df$y, col='steelblue') 

enter image description here

lines(df$q, df$y, col='red') 
lines(df$q[df$x==0], df$y[df$x==0], col = 'green') 

enter image description here

+0

サンディパンありがとうございました。 コードをオフにして、緑色の線が緑色になるように線を変更したい場合は、線を変更しますか?線(df $ q、df $ y、col = ifelse(d $ x == 0、 'green'、 'red)) – user1901959

+0

サンディパンありがとうございます – user1901959

2

点はゼロに等しい。

library(tidyr) 
library(ggplot2) 

df <- data.frame(q = seq(1:7), 
       x = c(12,18,21,19,0,0,0), 
       y = c(0,0,0,0, 21, 25, 23)) 

plot_data <- gather(df, variable, value, -q) 

plot_data <- plot_data[plot_data$value != 0,] 

ggplot(plot_data, aes(x = q, y = value)) + 
    geom_line(color = "black") + 
    geom_point(aes(color = variable)) 

enter image description here

+0

ジェイクにありがとう。 ggplotでこれを行う方法を知っているのは素晴らしいです – user1901959

関連する問題