我对R来说是全新的(来自MATLAB)。我试图生成由几个较小颜色区域组成的矩形图像。它基本上是一个温度计。它将位于透明/白色背景中,左侧有空间,用于指向某种颜色的线条/箭头,其中包含一个字符串,例如" XXXX坐在这里"。
我可以在MATLAB中完成它而不用太担心(新工作场所使用R),我只需定义一个绘图区域,用特定颜色填充矩形来绘制温度计,使用变量绘制一条水平线告诉我它放在温度计的哪个位置,然后在其左边添加一个名称字符串。然后我将该地块保存为图像,或者只是将其发送到我正在处理的模板中。
我在R中努力做到这一点(使用R studio)。这就是我到目前为止所拥有的:
require(grDevices)
## set up the plot region:
dev.off()
ACE <- par(bg = "transparent", usr = c(0, 51, 0, 451)) #make the plot window a certain size?
plot(x=NULL, y=NULL , type = "n", axes = F, xlab = "", ylab = "", xlim=c(0,51), ylim=c(0,450)) #set up the plot?
#draw rectangles for thermometer
rect(0, 0, 50, 148, col = "#c00000", border = "transparent") #red
rect(0, 148, 50, 225, col = "#ed7d31", border = "transparent") #orange
rect(0, 225, 50, 297, col = "#ffc000", border = "transparent") #gold
rect(0, 297, 50, 360, col = "#92d050", border = "transparent") #lgreen
rect(0, 360, 50, 450, col = "#00b050", border = "transparent") #dgreen
assign("ACE", ACE)
#set up png
png(filename = "ACE.png",
width = 5, height = 15, units = "cm", pointsize = 12,bg = "white", res = 300,family = "", restoreConsole = TRUE,type = c("windows", "cairo", "cairo-png"))
print(ACE) #send my plot to the png????
dev.off()
在&#34;情节中绘制正确高度的颜色&#34;在R工作室的标签,但似乎填补了整个情节,无论我告诉它什么saize我喜欢这个情节。我不确定如何预览它(la imshow())。 png输出是正确的尺寸,虽然是空的。
我真的很感激你能给我的任何帮助,我已经尝试过最后几个小时的独奏,并且只有这么远。
非常感谢, 亚历
答案 0 :(得分:1)
以下是在图像文件中打印的方法:
#set up png
png(filename = "ACE.png", width = 5, height = 15, units = "cm", pointsize = 12, res = 300)
par(bg = "transparent", usr = c(0, 51, 0, 451)) #make the plot window a certain size?
plot(x=NULL, y=NULL , type = "n", axes = F, xlab = "", ylab = "", xlim=c(0,51), ylim=c(0,450)) #set up the plot?
#draw rectangles for thermometer
rect(0, 0, 50, 148, col = "#c00000", border = "transparent") #red
rect(0, 148, 50, 225, col = "#ed7d31", border = "transparent") #orange
rect(0, 225, 50, 297, col = "#ffc000", border = "transparent") #gold
rect(0, 297, 50, 360, col = "#92d050", border = "transparent") #lgreen
rect(0, 360, 50, 450, col = "#00b050", border = "transparent") #dgreen
dev.off()