我正在使用 geom_linerange 函式繪制一些資料。根據資料集,這是 5-10 年的日常觀察。
在我的 Mac 上運行腳本時,線范圍 alpha 會根據每個圖中的觀察數量而變化。但是,我希望所有地塊都具有 alpha=1。在 geom_linerange 函式中顯式設定 alpha 對繪圖沒有影響 - 當繪制大量觀察值時,顏色仍然是透明的。
當我在 Windows 筆記本電腦上使用完全相同的腳本時,默認 alpha 為 1 時繪圖是正確的。
下面是一個最小的作業示例:
library(ggplot2)
library(gridExtra)
df1 = data.frame(name = c("A","B","C"),
Date = rep(seq(as.Date("2010-01-01"),as.Date("2018-01-01"),by=1),each=3),
value = runif(8769,-1,1))
df2 = data.frame(name = c("A","B","C"),
Date = rep(seq(as.Date("2010-01-01"),as.Date("2014-01-01"),by=1),each=3),
value = runif(4386,-1,1))
df3 = data.frame(name = c("A","B","C"),
Date = rep(seq(as.Date("2010-01-01"),as.Date("2011-01-01"),by=1),each=3),
value = runif(1098,-1,1))
Plot1 = ggplot()
geom_linerange(data=df1,aes(x=name,ymin=Date,ymax=Date 1,colour=value),size=15)
scale_colour_gradient2(low="red",mid="white",high="blue",midpoint=0,name = "Value")
theme_bw()
coord_flip()
xlab("Driver")
ylab("")
Plot2 = ggplot()
geom_linerange(data=df2,aes(x=name,ymin=Date,ymax=Date 1,colour=value),size=15)
scale_colour_gradient2(low="red",mid="white",high="blue",midpoint=0,name = "Value")
theme_bw()
coord_flip()
xlab("Driver")
ylab("")
Plot3 = ggplot()
geom_linerange(data=df3,aes(x=name,ymin=Date,ymax=Date 1,colour=value),size=15)
scale_colour_gradient2(low="red",mid="white",high="blue",midpoint=0,name = "Value")
theme_bw()
coord_flip()
xlab("Driver")
ylab("")
grid.arrange(Plot1,Plot2,Plot3)
下面是我的 Mac 上的輸出。具有最多觀察值的頂部圖具有最低的 alpha:

以下是我的 Windows 上的輸出 - 如您所見,所有圖都具有 alpha=1:

代碼通過 GitHub 存盤庫傳輸。
Unfortunately, I am absolutely stumped as to why this is occurring. Is this expected behaviour on a Mac, or is there something I am doing wrong?
Many thanks!
uj5u.com熱心網友回復:
這是高頻資料與圖形設備之間互動的結果,尤其是其抗鋸齒設定/功能。在這種情況下,我們嘗試使用(在我下面的示例中)僅約 600 像素的繪圖寬度繪制約 2,900 天的資料。每個像素代表大約 4 天的資料,抗鋸齒提供了更“模糊”的外觀,而沒有抗鋸齒的繪圖更好地顯示了資料的范圍(以顯示更少的資料為代價;我猜我們正在有效地看到每四天的資料)。
在 Windows 中,我相信 Plot 視窗的默認圖形設備是 Quartz,沒有抗鋸齒。Plot1 Plot2 在該設定下看起來像這樣:

如果我在 RStudio 全域設定中啟用抗鋸齒,我得到的結果與你的 Mac 結果相似,因為它的默認圖形設備使用抗鋸齒。


獲得目標的最簡單方法是將解析度提高到足以每天至少提供一個像素;這樣你就可以代表 100% 的資料并使用你的色標的全部范圍。您還可以輸出為矢量格式,svg以實作更高的有效解析度。
Alternatively, depending on the nature of your data and what you're trying to show, you might taking a rolling average across your days (I expect the result would be similar to the antialiased outputs), or grab a rolling max or min or SD, or some other summary measure which captures what you want more directly, but at a more digestible time granularity. You might also consider other geometries (like a line chart, or scatter plot, or a horizon plot) which are easier for a reader to map to values.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/359581.html
標籤:r windows macos ggplot2 alpha
