我有 7 個值,我試圖使用它來繪制geom_boxpot
,我希望只在圖例中顯示某些值,所以我在函式中使用breaks
引數,scale_linetype_manual
但它根本不繪制那些箱線圖,
箱線圖使用scale_linetype_manual
但是,如果我使用相同的代碼,scale_linetype_discrete
它可以正常作業并繪圖,并且只給我圖例中所需的值。但是,我無法使用values
引數控制函式中的線型。有沒有辦法為scale_linetype_discrete
函式添加值?
箱線圖使用scale_linetype_discrete
編輯-用假資料 代碼更新
> head(debug)
# A tibble: 6 × 4
subj time cond y
<dbl> <chr> <chr> <dbl>
1 1 one one_A 2
2 1 two two_A 1
3 1 two two_B 5
4 1 two two_C 0
5 1 three three_A 4
6 1 four four_A 4
> dput(debug)
structure(list(subj = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4), time = c("one",
"two", "two", "two", "three", "four", "four", "one", "two", "two",
"two", "three", "four", "four", "one", "two", "two", "two", "three",
"four", "four", "one", "two", "two", "two", "three", "four",
"four"), cond = c("one_A", "two_A", "two_B", "two_C", "three_A",
"four_A", "four_B", "one_A", "two_A", "two_B", "two_C", "three_A",
"four_A", "four_B", "one_A", "two_A", "two_B", "two_C", "three_A",
"four_A", "four_B", "one_A", "two_A", "two_B", "two_C", "three_A",
"four_A", "four_B"), y = c(2, 1, 5, 0, 4, 4, 2, 2, 4, 3, 0, 1,
5, 3, 1, 5, 4, 2, 0, 4, 4, 0, 0, 4, 2, 1, 5, 5)), row.names = c(NA,
-28L), spec = structure(list(cols = list(subj = structure(list(), class = c("collector_double",
"collector")), time = structure(list(), class = c("collector_character",
"collector")), cond = structure(list(), class = c("collector_character",
"collector")), y = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x600004737380>, class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"))
# Scale_linetype_discrete (would like to add linetype values to this)
ggplot()
geom_boxplot(debug,
mapping = aes(x = time,
y = y,
linetype = cond),
show.legend = TRUE)
scale_x_discrete(limits = c("one","two","three","four"),
labels = c("one","two","three","four"))
scale_linetype_discrete(name = "My legend",
breaks = c("two_A","two_B","two_C","four_A","four_B"),
labels = c("two_A","two_B","two_C","four_A","four_B"))
# Scale_linetype_manual (would like 'one' and 'three' to be actually plotted but not showing in my legend)
ggplot()
geom_boxplot(debug,
mapping = aes(x = time,
y = y,
linetype = cond),
show.legend = TRUE)
scale_x_discrete(limits = c("one","two","three","four"),
labels = c("one","two","three","four"))
scale_linetype_manual(name = "My legend",
breaks = c("two_A","two_B","two_C","four_A","four_B"),
labels = c("two_A","two_B","two_C","four_A","four_B"),
values = c("solid","dashed","solid","dashed","solid", "dashed","solid"))
uj5u.com熱心網友回復:
不確定您的代碼不起作用的原因。但是解決您的問題的一種選擇是使用命名向量將線型分配給cond
變數的類別:
library(ggplot2)
lty <- c("solid", "dashed", "solid", "dashed", "solid", "dashed", "solid")
names(lty) <- c("four_A", "four_B", "one_A", "three_A", "two_A", "two_B", "two_C")
ggplot()
geom_boxplot(debug,
mapping = aes(
x = time,
y = y,
linetype = cond
)
)
scale_x_discrete(
limits = c("one", "two", "three", "four"),
labels = c("one", "two", "three", "four")
)
scale_linetype_manual(
name = "My legend",
breaks = c("two_A", "two_B", "two_C", "four_A", "four_B"),
values = lty
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443262.html
上一篇:將矢量資料分組以標記折線圖
下一篇:修改缺少資料的分組條形圖顏色