简体   繁体   English

绘制线以分组点

[英]Plot lines to group points

This is the data from my csv file: 这是我的csv文件中的数据:

Tx, Varx, Scale, Val1, Val2
A, VAR1, 5, 516, 2
A, VAR1, 10, 447.4, 5
A, VAR1, 15, 294, 8
A, VAR1, 20, 217.2, 12

A, VAR2, 5, 675.4, 4
A, VAR2, 10, 423.2, 9
A, VAR2, 15, 276, 12
A, VAR2, 20, 200, 15

B, VAR1, 5, 624, 6
B, VAR1, 10, 465.2, 13
B, VAR1, 15, 315.2, 16
B, VAR1, 20, 234.8, 18

B, VAR2, 5, 518.8, 8
B, VAR2, 10, 443, 17
B, VAR2, 15, 278.4, 20
B, VAR2, 20, 217.8, 24

I want to plot lines (not only points) to distingish between Varx and Tx values. 我想绘制线(不仅是点)以区分Varx和Tx值。 I'm trying to plot with this code: 我正在尝试使用以下代码进行绘制:

data_table = read.csv("PATH/file.csv",check.names=FALSE,header=T,sep=",")
data_table$NScale <- as.numeric(as.character((data_table$Scale)))
ggplot(data_table, aes(x=NScale, y=Val2, colour=Tx, shape=Varx, linetype=Varx, group=Tx)) + geom_point()

在此处输入图片说明

When I try to plot lines for joining blue triangules, red triangules, red circles, blue circles, using geom_line(), geom_path() an error message is displayed: 当我尝试使用geom_line(),geom_path()绘制用于连接蓝色三角形,红色三角形,红色圆圈,蓝色圆圈的线时,显示错误消息:

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

How I can create the lines grouping by shape and color? 如何创建按形状和颜色分组的线条? I've tried several ways, but still do not get it. 我已经尝试了几种方法,但是仍然不明白。 What is the problem? 问题是什么?

You should try to make a new "grouping" variable and separate how you do the plot for the "points" and the "line". 您应该尝试创建一个新的“分组”变量,并分开处理“点”和“线”的图。

I've done this here using dplyr but you could do it using base R as well: 我在这里使用dplyr进行了此dplyr但您也可以使用base R进行此操作:

library(dplyr)

data_table <- data_table %>% 
  rowwise() %>%
  mutate(TxVarxgroup = paste0(Tx, Varx, collapse=""))

ggplot(data_table) + 
  geom_point(aes(x=NScale, y=Val2, colour=Tx, shape=Varx)) + 
  geom_line(aes(x=NScale, y=Val2, group=TxVarxgroup))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM