如何调整传奇位置,颜色和边界框?

发布于 2025-01-22 10:44:02 字数 744 浏览 2 评论 0原文

有什么方法可以自动找到传说的最佳位置?如何在传说之前显示带有颜色的矩形并调整传奇的边框?以下是我的代码,结果

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"))

enter image description here

You may notice that the very left line is not straight. How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转瞬即逝 2025-01-29 10:44:02

使用其他设备,例如pngpdfjpgpostscript,...

您可能正在使用rstudio通常是图中显示在窗口中。通常,用户调整此窗口,直到绘图具有所需的格式。但是,那时可能会扭曲传说和文字。您可能只是重新运行代码,但这很乏味,如果您用指定的维度导出图,也不能保证该图在此 preview 中看起来像,尤其是传说和文本。

此预览是“ rstudiogd”设备(以某种方式与png设备结合使用)。您可以使用dev.list()在创建绘图后看到它。

获得稳定图结果的解决方案是使用其他设备,例如pngpdfjpgPostscript等等,您指定呼叫中的尺寸,而不是通过移动鼠标。立即打开保存的图以检查结果。在Linux上,您可以保持保存的地块打开,它将刷新本身。

请注意,您需要启动新设备,绘图,然后使用device.off()停止设备。否则,将不会存储绘图,并且将永远发送到新设备,直到您关闭(或退出R)为止。

如何在磁盘上保存图作为图像?

基本设置

另请参阅此问题 使用PNG设备。

png('file', .)  ## start device
plot(.)  ## create plot
lines(.)
...
device.off()  ## stop device

如果您与设备发生冲突,只需使用graphics.off()关闭所有设备。

您的图

png('myplot1234.png', 480, 480)  ## start device

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"), 
       pch=15, 
       legend=c("Majority", "Minority", "Infinity"))

dev.off()  ## stop device

应将图作为'myplot1234.png'保存在您的工作目录中。您也可以指定课程'PATH/MYPLOT1234.PNG'的路径。 (请注意。您需要在图例中指定一些pCH =,否则没有显示颜色。)

“在此处输入图像说明”


data:

questions <- seq(1, 50, by=1)
probs <- dgeom(questions, prob=0.25)

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 a png device). You can see it using dev.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.

png('file', .)  ## start device
plot(.)  ## create plot
lines(.)
...
device.off()  ## stop device

If you get a clash with your devices, just close all devices using graphics.off().

Your plot

png('myplot1234.png', 480, 480)  ## start device

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"), 
       pch=15, 
       legend=c("Majority", "Minority", "Infinity"))

dev.off()  ## stop device

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 some pch= in the legend, otherwise no colors are shown.)

enter image description here


Data:

questions <- seq(1, 50, by=1)
probs <- dgeom(questions, prob=0.25)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文