1

I have the following data which I have graphed in a double line graph. I want to add an asterisk whenever significance is "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 to be what I want. I tried nudge_y but it didn't work.

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")

enter image description here

Whenever I want to add significance asterisks to a bar graph, it's easy. I put in:

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

However, this doesn't work at all for line graphs for some reason. Why not?

user3710004
  • 511
  • 1
  • 6
  • 15

2 Answers2

2

1) Positioning of significance asterisk

The simple way to get the significance asterisks above the line or points of your main data is by adjusting the y value in the aes call. See answer.

You could pre-process this in your dataset before generating the plot by creating another variable for example sig_y with mutate if you are using dplyr

2) Typeface or font family: Georgia.

This is a bit more involved and I found these links helpful: Changing fonts in ggplot2 and this one: https://github.com/wch/extrafont

Making additional typefaces accessible to 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

Packages

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

Data (By the way, this is a useful way to provide data for any future questions as it makes it easy for members of the SO community to reproduce your question.)

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"))

Script for 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

enter image description here

Peter
  • 11,500
  • 5
  • 21
  • 31
0

If I may offer an adaptation of the solution provided by Peter that lets you add text other than one *:

Here we again generate the 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"))

And the plotting code:

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")

instead of using geom_point() you can use geom_text() like so:

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="*"))

At label= you can provide any string you would like to have above the dots, if that by "n.s." or multiple **.

ktm
  • 67
  • 6