[問題] ggpattern 做圖問題

作者: vetvet ( )   2020-08-23 12:18:10
[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
入門(寫過其他程式,只是對語法不熟悉)
[問題敘述]:
各位前輩好,小弟正在整理實驗的資料進行論文撰寫,
數據的圖表主要以 ggplot2 進行繪製,在柱狀圖中想填充線條(直線/横線等),
因此使用 ggpattern 這個套件,在製做圖表的過程中,突然遇到無法做圖的問題,
特別是在使用 pattern = "magick" 時會出現 error,
刪掉這項或用其他圖形選項,就可以出圖,但又無法自訂填充 pattern,
error message 的內容又不知如何修改…
想請教應如何解決這個問題,謝謝
[程式範例]:
#使用套件
library(ggplot2)
library(ggpattern)
# 資料表單
# 表單項目包含 Group2, WPC, mean, SE
a <- data.frame(Group2 = c("C", "C", "C", "C", "A1", "A1", "A1", "A1",
"A1", "B1", "B1", "B1", "B1", "B1", "D", "D", "D", "D", "D"),
WPC = c(-4, 0, 1, 3, -4, 0, 1, 2, 3, -4, 0, 1, 2, 3, -4, 0,1, 2, 3),
mean = c(0.46600, 0.84100, 1.41200, 3.11100, -0.51725,0.11100,
0.00075, 1.12425, 2.17350, -0.19225, 0.83825,1.73100,
2.72275, 2.86675, -0.74175, 3.82575, 1.64250, 3.70875, 3.98275),
SE = c(0.2234327, 0.1664126, 0.6513623, 0.4452928, 0.7768604,
0.8240675, 0.9581961, 1.2281746, 0.2186948, 0.2168496,
0.9284905, 0.9330518, 0.9475290, 0.2215267, 0.4349041,
1.7227386, 0.4133089, 0.5498973, 0.5349533))
# 作圖
ggplot(a, aes(factor(WPC), mean, gorup = Group2))+
geom_col_pattern(aes(pattern_type=Group2), pattern="magick",
pattern_fill="black", pattern_color="black", fill="white", color="black",
width = 0.5, position = position_dodge(0.5)) +
scale_pattern_type_discrete(choices = c("vertical", "horizontal",
"hs_vertical", "hs_horizontal"))
# 錯誤訊息
Error in png(png_file, width = width, height = height) :
invalid 'width' or 'height'
此外: Warning messages:
1: In png(png_file, width = width, height = height) :
'width=11, height=19' are unlikely values in pixels
2: In png(png_file, width = width, height = height) :
'width=11, height=4' are unlikely values in pixels
3: create_magick_pattern_img(): zero size
4: In FALSE :
create_pattern_array(): Expecting a numeric RGBA array with dim = c(0, 11,
4) but instead got a "array" of type double with dimensions c(10L, 11L, 4L)
[環境敘述]:
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
相關套件版本
ggplot2_3.3.2
ggpattern_0.1.3
[關鍵字]:
ggpattern
作者: locka (locka)   2020-08-23 15:00:00
你X軸打算放的是WPC還是Group2?問題應該是出在你dataframe你X軸打算放的是WPC還是Group2?問題應該是出在你dataframe沒有整理成正確的格式。https://i.imgur.com/V0v9DFp.jpghttps://i.imgur.com/bD33BYE.jpg要把wpc欄位填進去是可行的,可是wpc的類別數必須要跟Group2的類別數一致。你資料裡wpc有5種可是Group2只有4種

Links booklink

Contact Us: admin [ a t ] ucptt.com