如何调整传奇位置,颜色和边界框?
有什么方法可以自动找到传说的最佳位置?如何在传说之前显示带有颜色的矩形并调整传奇的边框?以下是我的代码,结果
questions <- seq(1, 50, by=1)
probs <- dgeom(questions, prob=0.25)
plot(questions, probs, type='l', ylab='prob')
polygon(c(questions[15:50], 50, 15),
c(probs[15:50], 0, 0), col="red")
polygon(c(questions[8:15], 15, 8),
c(probs[8:15], 0, 0), col="blue")
polygon(c(questions[8:15], 15, 8),
c(probs[8:15], 0, 0), col="blue")
polygon(c(questions[0:8], 8, 0),
c(probs[0:8], 0, 0), col="green")
legend("topright",
col=c("green", "blue", "red"),
legend=c("Majority", "Minority", "Infinity"))
您可能会注意到非常左行不直。我该如何解决?
Is there a way that R will automatically find the best position for the legend? How to display a rectangle with color before the legend and adjust the border of the legend? Below is my code and the result
questions <- seq(1, 50, by=1)
probs <- dgeom(questions, prob=0.25)
plot(questions, probs, type='l', ylab='prob')
polygon(c(questions[15:50], 50, 15),
c(probs[15:50], 0, 0), col="red")
polygon(c(questions[8:15], 15, 8),
c(probs[8:15], 0, 0), col="blue")
polygon(c(questions[8:15], 15, 8),
c(probs[8:15], 0, 0), col="blue")
polygon(c(questions[0:8], 8, 0),
c(probs[0:8], 0, 0), col="green")
legend("topright",
col=c("green", "blue", "red"),
legend=c("Majority", "Minority", "Infinity"))
You may notice that the very left line is not straight. How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用其他设备,例如
png
,pdf
,jpg
,postscript
,...您可能正在使用rstudio通常是图中显示在图窗口中。通常,用户调整此窗口,直到绘图具有所需的格式。但是,那时可能会扭曲传说和文字。您可能只是重新运行代码,但这很乏味,如果您用指定的维度导出图,也不能保证该图在此 preview 中看起来像,尤其是传说和文本。
此预览是
“ rstudiogd”
设备(以某种方式与png
设备结合使用)。您可以使用dev.list()
在创建绘图后看到它。获得稳定图结果的解决方案是使用其他设备,例如
png
,pdf
,jpg
,Postscript
等等,您指定呼叫中的尺寸,而不是通过移动鼠标。立即打开保存的图以检查结果。在Linux上,您可以保持保存的地块打开,它将刷新本身。请注意,您需要启动新设备,绘图,然后使用
device.off()
停止设备。否则,将不会存储绘图,并且将永远发送到新设备,直到您关闭(或退出R)为止。: 如何在磁盘上保存图作为图像?
基本设置
另请参阅此问题 使用
PNG
设备。如果您与设备发生冲突,只需使用
graphics.off()
关闭所有设备。您的图
应将图作为
'myplot1234.png'
保存在您的工作目录中。您也可以指定课程'PATH/MYPLOT1234.PNG'
的路径。 (请注意。您需要在图例中指定一些pCH =
,否则没有显示颜色。)data:
Use a different device, such as
png
,pdf
,jpg
,postscript
, ...You are probably using RStudio were usually plots are shown in the Plots window. Usually the user adjusts this window until the plot has the desired format. However, legends and text among other things might be distorted then. You may just rerun your code but this is tedious and if you export the plot with specified dimensions, it's also not guaranteed that the plot will look like in this preview, especially legends and text.
This preview is the
"RStudioGD"
device (somehow combined with apng
device). You can see it usingdev.list()
after you created a plot.The solution to get a stable plot result is using a different device such as
png
,pdf
,jpg
,postscript
etc., where you specify the dimensions in the call rather than by moving the mouse. Open the saved plot now to check the result. On Linux you may keep the saved plot open and it will refresh itself.Note, that you need to start the new device, plot, then stop the device using
device.off()
. Otherwise the plot won't be stored and new plots will be sent to the new device forever until you close it (or quit R).Also see this question: How to save a plot as image on the disk?
Basic setup
Here using
png
device.If you get a clash with your devices, just close all devices using
graphics.off()
.Your plot
The plot should be saved as
'myplot1234.png'
in your working directory. You may also specify a path of course'path/myplot1234.png'
. (Notice. that you need to specify somepch=
in the legend, otherwise no colors are shown.)Data: