簡體   English   中英

在 ggplot2 的折線圖中添加顯着性星號

[英]Add significance asterisk to line graph in ggplot2

我有以下數據,我用雙線圖繪制了這些數據。 當重要性為“y”時,我想添加一個星號。

structure(list(Year = c(0, 1, 2, 3, 0, 1, 2, 3), school_type = c("Management Change", 
"Management Change", "Management Change", "Management Change", 
"Full Closure", "Full Closure", "Full Closure", "Full Closure"
), pct_change = c(3.7, 2, 0.8, -1.1, 9.2, 6.9, 5.4, 6.6), significance = c("y", 
"n", "n", "n", "y", "y", "y", "y")), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))

I used the following code to create asterisks (after consulting R ggplot2: add significance level to line plot ), but the problem is that I can't move those asterisks up to get them out of the point of the points, or change the font成為我想要的。 我試過 nudge_y 但它沒有用。

ggplot(fig4, aes(x=Year, y=pct_change, color=school_type, group=school_type)) +
  geom_line(size=1) +
  geom_point(size=3) +
  theme_bw(base_family = "Georgia") +
  geom_point(data = fig4[fig4$significance == "y", ], shape = "*", size=4.233, color="black")

在此處輸入圖像描述

每當我想在條形圖中添加重要性星號時,這很容易。 我輸入:

ggplot(df, aes(label = ifelse(significance == "y", "*",""))

但是,由於某種原因,這對於折線圖根本不起作用。 為什么不?

1) 重要星號的定位

獲取主數據線或點上方的重要性星號的簡單方法是調整 aes 調用中的 y 值。 見答案。

如果您使用dplyr ,您可以在生成 plot 之前在數據集中對此進行預處理,方法是創建另一個變量,例如 sig_y 和 mutate

2) 字體或字體系列:Georgia。

這有點涉及,我發現這些鏈接很有幫助: 在 ggplot2 中更改 fonts和這個: https://github.com/wch/ex

使 R 可以訪問其他字體:

library(extrafont) # package for accessing fonts on your computer assuming you are running windows
font_import() # imports all the fonts on your computer; may take several minutes
fonts <- fonttable() # tabulates the extracted fonts allows you to check what fonts you have and you can confirm that Georgia is in the mix

套餐

library(ggplot2)
library(tibble)
library(extrafont)

數據(順便說一句,這是為任何未來問題提供數據的有用方法,因為它使 SO 社區的成員可以輕松地重現您的問題。)

fig4 <- tibble(Year = c(0, 1, 2, 3, 0, 1, 2, 3),
               school_type = c("Management Change", "Management Change", "Management Change", "Management Change", 
                               "Full Closure", "Full Closure", "Full Closure", "Full Closure"), 
               pct_change = c(3.7, 2, 0.8, -1.1, 9.2, 6.9, 5.4, 6.6), 
               significance = c("y", "n", "n", "n", "y", "y", "y", "y"))

plot 的腳本

loadfonts(device = "win") # to give access to non-ggplot fonts

ggplot(fig4, aes(x=Year, y=pct_change, color=school_type, group=school_type)) +
  geom_line(size=1) +
  geom_point(size=3) +
  theme_bw(base_family = "Georgia")+
  geom_point(data = fig4[fig4$significance == "y", ], aes(Year, pct_change + 0.25), shape = "*", size=4.233, color="black")

Output

在此處輸入圖像描述

如果我可以提供彼得提供的解決方案的改編版,允許您添加除一個 * 以外的文本:

這里我們再次生成dataframe:

fig4 <- tibble(Year = c(0, 1, 2, 3, 0, 1, 2, 3),
school_type = c("Management Change", "Management Change", "Management Change", "Management Change",
"Full Closure", "Full Closure", "Full Closure", "Full Closure"),
pct_change = c(3.7, 2, 0.8, -1.1, 9.2, 6.9, 5.4, 6.6),
significance = c("y", "n", "n", "n", "y", "y", "y", "y"))

和繪圖代碼:

ggplot(fig4, aes(x=Year, y=pct_change, color=school_type, group=school_type)) +
geom_line(size=1) +
geom_point(size=3) +
theme_bw(base_family = "Georgia")

而不是使用 geom_point() 你可以使用 geom_text() 像這樣:

ggplot(fig4, aes(x=Year, y=pct_change, color=school_type, group=school_type)) +
geom_line(size=1) +
geom_point(size=3) +
theme_bw(base_family = "Georgia")+
geom_point(data = fig4[fig4$significance == "y", ], aes(Year, pct_change + 0.25, label="*"))

在 label= 處,您可以提供您希望在點上方包含的任何字符串,如果是“ns”或多個 **。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM