Deleting Line from Plot

浪子不回头ぞ 提交于 2019-12-22 06:59:46

问题


Just a quick question: I'm trying to plot a graph in R and I have covered how to do that, but how do I delete a line I have just created? For instance:

x <- c(1, 2, 4, 5, 6.7, 7, 8, 10 )
y <- c(40, 30, 10, 20, 53, 20, 10, 5)

plot(x,y,main="X vs Y", xlab="X", ylab="Y")

lines(x,y,col="black",lty="dotted") 

This produces a nice graph. However, say I would like to delete the line I created previously (or perhaps the points as well?!) how should I go about doing it?


回答1:


The trick to erasing in R base is to redraw everything except what you want to erase in a new plot

so if you:

plot(x,y,main="X vs Y", xlab="X", ylab="Y")
lines(x,y,col="black",lty="dotted") 

then decide that you dont want the line then you:

plot(x,y,main="X vs Y", xlab="X", ylab="Y")

Then if you want to erase everthing then you

plot.new()



回答2:


In order to delete a line you just delete the line command and rerun the rest of your commands.

You should think of your plot as your code. You save the code because it's even more informative about what the plot is than the actual plot. Unless you have a LOT of things to draw in your plot it's relatively trivial to just re-plot everything. You may need to re-plot dozens of times until it's exactly what you want. FYI, deep down, this is exactly what some GUI based graphing programs do when you tweak things (depends on what you tweak). So, it's not like R is particularly special in this sense.

Alternatively, save the plot as a vector graphic (i.e. PDF), open in a compatible vector graphic drawing program (i.e. Illustrator), and tweak to your hearts content.



来源:https://stackoverflow.com/questions/11569339/deleting-line-from-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!