简体   繁体   English

R.如何避免连接点图中点的线

[英]R. How to avoid lines connecting dots in dotplot

I made a plot using plot() using RStudio. 我使用RStudio使用plot()绘制了一个图。 在此处输入图片说明

x = X$pos
y = X$anc
z = data.frame(x,y)

#cut in segments
my_segments = c(52660, 106784, 151429, 192098, 233666, 
                273857, 307933, 343048, 373099, 408960, 
                441545, 472813, 497822, 518561, 537471, 
                556747, 571683, 591232, 599519, 616567, 
                625727, 633744)
my_cuts = cut(x,my_segments, labels = FALSE)
my_cuts[is.na(my_cuts)] = 0

This is the code:

#create subset of segments
z_alt = z
z_alt[my_cuts %% 2 == 0,] = NA

#plot green, then alternating segments in blue
plot(z, type="p", cex = 0.3,pch = 16,
     col="black", 
     lwd=0.2,
     frame.plot = F,
     xaxt = 'n', # removes x labels,
     ylim = c(0.3, 0.7),
     las = 2,
     xlim = c(0, 633744),
     cex.lab=1.5, # size of axis labels
     ann = FALSE, # remove axis titles
     mgp = c(3, 0.7, 0)) 

lines(z_alt,col="red", lwd=0.2)

# adjust y axis label size
par(cex.axis= 1.2, tck=-0.03)

If you see, some black dots are separated, but other black dots have red connecting lines. 如果看到的话,一些黑点是分开的,但是其他黑点具有红色的连接线。 Does anyone know how to remove these annoying lines?. 有谁知道如何删除这些烦人的线条? I just want black and red dots. 我只想要黑点和红点。 Many thanks 非常感谢

The problem here is that you are using lines to add your z_alt . 这里的问题是您正在使用lines添加z_alt As the name of the function suggests, you will be adding lines. 顾名思义,该函数将添加行。 Use points instead. 使用points代替。

z <- runif(20,0,1)
z_alt <- runif(20,0.8,1.2)
plot(z, type="p", col="black", pch = 16, lwd=0.2, ylim = c(0,1.4)) 
points(z_alt, col = "red", pch = 16, lwd = 0.2)

there is no need to call the points in a second function. 无需在第二个函数中调用这些点。 you can try to directly set the color in the plot function using a color vector. 您可以尝试使用颜色向量在绘图功能中直接设置颜色。

#  create some data as you have not provided some
set.seed(123)
df <- data.frame(x=1:100,y=runif(100))

# some sgment breaks
my_segments <- c(0,10,20,50,60)
gr <- cut(df$x, my_segments,labels = FALSE, right = T)
gr[is.na(gr)] <- 0

# create color vector with 1 == black, and 2 == red
df$color <- ifelse(gr %% 2 == 0, 1, 2)

# and the plot
plot(df$x, df$y, col = df$color, pch = 16)

在此处输入图片说明

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

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