ggplot2: geom_area with factorial x-axis - r

I am trying to make a stacked area plot. My x-axis is age categories, but I want to connect them as a continuous scale.
I have number of diagnoses on the y (filled by diagnosis) and age on the x.
subset of data:
structure(list(diag = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Hjernerystelse",
"Lungesygdomme"), class = "factor"), age = structure(c(1L, 2L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 3L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 1L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 3L,
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L), .Label = c("0 år", "1-4 år",
"5-9 år", "10-14 år", "15-19 år", "20-24 år", "25-29 år", "30-34 år",
"35-39 år", "40-44 år", "45-49 år", "50-54 år", "55-59 år", "60-64 år",
"65-69 år", "70-74 år", "75-79 år", "80-84 år", "85- år"), class = "factor"),
n = c(15L, 89L, 87L, 71L, 46L, 32L, 26L, 24L, 32L, 40L, 74L,
55L, 39L, 19L, 38L, 27L, 24L, 14L, 23L, 291L, 2170L, 267L,
269L, 234L, 244L, 256L, 336L, 432L, 638L, 458L, 792L, 1010L,
1401L, 2088L, 2087L, 1815L, 1767L, 1995L)), row.names = c(NA,
-38L), .Names = c("diag", "age", "n"), class = c("tbl_df", "tbl",
"data.frame"))
The following gives a useful plot, but doesn't conserve the categories on the x-axis:
plot1 <- ggplot(foo, aes(age, n)) +
geom_area(aes(x=as.numeric(factor(age)), fill=diag))
Is there a simple way to use the geom_area on factorial data, or just a way to show the levels on the x-axis.
An other problem is that by data have 29 different diagnoses giving this result:

Try this approach:
library(directlabels)
ggplot(foo, aes(x=as.numeric(factor(age)), y = n, fill=diag)) +
geom_area() +
scale_x_discrete(labels = levels(foo$age)) +
geom_dl(aes(label = diag), list("top.points", cex = .6)) +
guides(fill = FALSE)
Edit:
With regards to your comment, try this
download.file("https://dl.dropboxusercontent.com/u/12226044/admissions.Rdata",
destfile = fn <- file.path(tempdir(), "admissions.Rdata"),
mode = "wb")
load(fn)
library(ggplot2)
library(directlabels)
library(dplyr)
shaped %>%
group_by(diag, age) %>%
summarise(n = mean(n)) %>%
ggplot(aes(x=as.numeric(factor(age)), y = n, fill=diag)) +
geom_area(position = "stack") +
scale_x_discrete(labels = levels(shaped$age), expand = c(.1, .1)) +
geom_dl(aes(label = diag, color = diag), position = "stack", list("last.bumpup", rot=-30, cex = .5)) +
guides(fill = FALSE, colour = FALSE)

Related

xAxis order of R highcharter column plot

With the following data frame:
dta <- structure(list(sociodemographic_var = structure(c(3L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 26L, 18L, 20L, 21L, 26L, 13L, 16L, 21L, 22L, 26L, 26L,
9L, 13L, 17L, 18L, 20L, 21L, 23L, 26L, 20L, 26L), levels = c("1st grade",
"2nd grade", "3rd grade", "4th grade", "5th grade", "6th grade",
"7th grade", "8th grade", "9th grade", "10th grade", "11th grade",
"12th grade, no diploma", "High school graduate", "GED or equivalent",
"Some college, no degree", "Less than 1 year of college credit/post-secondary education (or less than 10 classes)",
"One year or more of college credit, no degree", "Associate degree: Occupational, Technical, or Vocational",
"Associate degree: Academic Program", "Bachelor's degree (ex. BA, AB, BS, BBS)",
"Master's degree (ex. MA, MS, MEng, MEd, MBA)", "Professional School degree (ex. MD, DDS, DVN, JD)",
"Doctoral degree (ex. PhD, EdD)", "Refused to answer", "Don't Know",
"unknown"), class = "factor"), event = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
3L, 3L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 7L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 11L, 11L), levels = c("Baseline", "0.5 Year", "1 Year",
"1.5 Year", "2 Year", "2.5 Year", "3 Year", "3.5 Year", "4 Year",
"4.5 Year", "5 Year", "5.5 Year", "6 Year", "Screener"), class = "factor"),
visit_type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), levels = c("on-site", "hybrid", "remote", "unknown"), class = "factor"),
n = c(2L, 13L, 5L, 9L, 15L, 18L, 26L, 25L, 192L, 27L, 485L,
224L, 183L, 1011L, 666L, 55L, 78L, 3L, 9L, 1L, 1L, 2L, 208L,
1L, 1L, 1L, 1L, 126L, 28L, 1L, 1L, 2L, 2L, 3L, 4L, 1L, 543L,
1L, 300L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-39L))
I would assume that, generating a highcharter bar plot with:
library(highcharter) # v0.9.4
dta |>
hchart(type = "column", hcaes(x = "event", y = "n", group = "sociodemographic_var")) |>
hc_yAxis(title = list(text = "%"), max = 115, endOnTick = FALSE, stackLabels = list(enabled = TRUE)) |>
hc_xAxis(title = "") |>
hc_plotOptions(series = list(stacking = "percent"))
the xAxis would be ordered by levels(dta$event):
levels(dta$event)
[1] "Baseline" "0.5 Year" "1 Year" "1.5 Year" "2 Year" "2.5 Year" "3 Year" "3.5 Year" "4 Year" "4.5 Year" "5 Year" "5.5 Year"
[13] "6 Year" "Screener"
But the ordering is different and neither alphabetical nor based on the total number of values:
I am interested to understand why it's the case and how to set the order right.
You can add categories to your hc_xAxis to make an order like this:
library(highcharter)
dta |>
hchart(type = "column", hcaes(x = "event", y = "n", group = "sociodemographic_var")) |>
hc_yAxis(title = list(text = "%"), max = 115, endOnTick = FALSE, stackLabels = list(enabled = TRUE)) |>
hc_xAxis(title = "", categories = levels(dta$event)) |>
hc_plotOptions(series = list(stacking = "percent"))
Output:

Cannot plot the correct x-axis in ggplot2

I am plotting the following data using ggplot2 in R.
dat<-structure(list(Month = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L,
8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
12L, 12L, 12L, 12L), grp1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L, 1L, 2L, 3L, 4L), .Label = c("(-Inf,2]", "(2,7]", "(7,14]",
"(14, Inf]"), class = "factor"), n = c(71L, 59L, 36L, 10L, 55L,
73L, 18L, 10L, 97L, 82L, 22L, 5L, 120L, 79L, 15L, 2L, 140L, 62L,
15L, 174L, 60L, 11L, 188L, 71L, 2L, 183L, 53L, 2L, 211L, 50L,
2L, 171L, 69L, 7L, 1L, 98L, 85L, 13L, 6L, 72L, 62L, 24L, 9L)), class
= "data.frame", row.names = c(NA,-43L))
Here's my script:
library(ggplot2)
p<-ggplot(data=test,aes(Month, n, fill = grp1))
p<- p + geom_col()
p <- p + theme(panel.background=element_rect(fill="white"),
plot.margin = margin(0.5,0.5,0.5,0.5, "cm"),
panel.border=element_rect(colour="black",fill=NA,size=1),
axis.line.x=element_line(colour="black"),
axis.line.y=element_line(colour="black"),
axis.text=element_text(size=20,colour="black",family="sans"),
axis.title=element_text(size=20,colour="black",family="sans"),
legend.position = "right", legend.key = element_rect(fill = 'white'))
p <- p + scale_y_continuous(limits = c(0,300),breaks=c(seq(0,300,50)), expand=c(0,0))
p <- p + scale_x_discrete(breaks=c(seq(1,12,1)),labels=c("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"),expand=c(0,0))
p <- p + labs(x = "Month", y = "Number of Days")
Here's the output:
Why is it that I cannot plot the x-axis values?
If I don't set the scale_x_discrete, the plot will look like this:
Any ideas on how to solve this?
I'll appreciate any help.
If you want the Month name along the xaxis, then you can add in as.factor(Month) to your ggplot script. Heres an example:-
p<-ggplot(data=dat,aes(as.factor(Month), n, fill = grp1))
p<- p + geom_col()
p <- p + theme(panel.background=element_rect(fill="white"),
plot.margin = margin(0.5,0.5,0.5,0.5, "cm"),
panel.border=element_rect(colour="black",fill=NA,size=1),
axis.line.x=element_line(colour="black"),
axis.line.y=element_line(colour="black"),
axis.text=element_text(size=20,colour="black",family="sans"),
axis.title=element_text(size=20,colour="black",family="sans"),
legend.position = "right", legend.key = element_rect(fill = 'white'))
p <- p + scale_y_continuous(limits = c(0,300),breaks=c(seq(0,300,50)), expand=c(0,0))
p <- p + scale_x_discrete(breaks=c(seq(1,12,1)),labels=c("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"),expand=c(0,0))
p <- p + labs(x = "Month", y = "Number of Days")
p
Which gives you this:-

Plotting labels of Points outside of Polygon with ggplot2/ggrepel

I am trying to do a map of my study areas using ggplot2 as I have seen some good outputs. I attached what I got from the following codes
The shapefile can be downloaded with this link.
library(ggplot2)
library(ggrepel)
points <- structure(list(city = structure(
c(
9L, 1L, 1L, 1L, 7L, 1L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 8L, 1L, 1L, 5L, 1L, 4L, 1L, 1L, 6L),
.Label = c("", "Adelaide", "Brisbane", "Canberra", "Darwin", "Hobart", "Melbourne", "Perth", "Sydney"),
class = "factor"),
site = structure(c(19L, 20L, 21L, 22L, 14L, 15L, 5L, 6L, 7L, 8L, 2L, 3L, 4L, 16L, 17L, 18L, 12L, 13L, 9L, 10L, 11L, 1L),
.Label = c("", "ADL1", "ADL2", "ADL3", "BNE1", "BNE2", "BNE3", "BNE4", "CBR1", "CBR2", "CBR3", "DRW1", "DRW2", "MEL1", "MEL2", "PER1", "PER2", "PER3", "SYD1",
"SYD2", "SYD3", "SYD4"), class = "factor"),
station = structure(c(8L, 5L, 12L, 16L, 10L, 3L, 22L, 18L, 17L, 20L, 2L, 14L, 11L, 4L, 7L, 19L, 15L, 21L, 6L, 9L, 13L, 1L),
.Label = c("", "Adelaide CBD", "Alphington", "Caversham", "Chullora", "Civic", "Duncraig", "Earlwood", "Florey", "Footscray", "Le Fevre 2", "Liverpool", "Monash", "Netley", "Palmerston", "Richmond", "Rocklea", "South Brisbane", "South Lake", "Springwood", "Winnellie", "Woolloongabba"), class = "factor"),
latitude = c(-33.9178, -33.8939, -33.9328, -33.6183, -37.8048, -37.7783, -27.4975, -27.4848, -27.5358, -27.6125, -34.9289, -34.9438, -34.7913, -31.9505, NA, NA, -12.50779, -12.4243233, -35.285307, -35.220606, -35.418302, -42.8821),
longitude = c(151.1347, 151.045, 150.9058, 150.7458, 144.8727, 145.0306, 153.035, 153.0321, 152.9934, 153.1356, 138.6011, 138.5492, 138.498, 115.8605, NA, NA, 130.94853, 130.8933502, 149.131579, 149.043539, 149.094018, 147.3272)),
.Names = c("city", "site", "station",
"latitude", "longitude"), class = "data.frame", row.names = c(NA, -22L))
AUS<-readRDS("gadm36_AUS_1_sp.rds")
ggplot() + geom_polygon(data = AUS, aes(x=long, y = lat, group = group, size=0.01),
fill = NA, color = "black") +
geom_point(data=points, aes(x=longitude, y=latitude), color ="blue", size=0.5) +
coord_fixed(1) +
geom_label_repel(data = points, aes(x=longitude, y=latitude, label=city),
box.padding = 1.2, point.padding = 1) +
theme_classic() + ylim(-60,0)+ xlim(100,180) + scale_size_identity()
Australia Map and my study areas:
Is there a way I can enlarge/expand the polygon so my points would be separated? Also can I direct all labels outside the polygon?

grid.arrange - make grid higher / extend

I would kindly like to ask for your help. I am trying to extend the graph grid below in order to fully display the three graphs. Everywhere I looked, I couldn't find any clues how to resize the actual grid, not each of the plots within them.
This is the code I am using:
p1 <- df %>%
filter(df$economic_area == "Poorer areas") %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Donations per candidate from poorer areas')
p2 <- df %>%
filter(df$economic_area == "Middle income areas") %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Donations per candidate from middle income areas')
p3 <- df %>%
filter(df$economic_area == "Upper class areas") %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Donations per candidate from upper class areas')
grid.arrange(p1, p2, p3, nrow = 3, heights = c(10, 0.65, 0.65))
Thanks a lot for your help and best regards,
Trgovec
edit:
I have tried using facets as well, but it does not work.
p <- df %>%
group_by(cand_nm) %>%
summarise(sum = sum(contb_receipt_amt)) %>%
arrange(desc(sum)) %>%
ggplot(aes(x = reorder(cand_nm, -sum), y=sum)) + geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 55, hjust = 1)) +
scale_y_continuous(labels = comma) +
labs(x='Candidate',
y='Recieved donations in USD',
title='Recieved donations per candidate')
p + facet_grid(. ~ df$Median_household_income)
this is the error:
Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(536L, 1279L, 380L, :
replacement has 618217 rows, data has 25
EDIT:
here is the reproducable dataset:
> dput(test)
structure(list(cand_nm = structure(c(4L, 4L, 20L, 23L, 1L, 20L,
19L, 5L, 23L, 1L, 12L, 3L, 19L, 2L, 5L, 8L, 16L, 6L, 12L, 3L,
4L, 15L, 24L, 22L, 11L, 2L, 17L, 6L, 9L, 17L, 11L, 19L, 8L, 16L,
22L, 20L, 13L, 24L, 5L, 23L, 15L, 25L, 9L, 14L, 9L, 10L, 21L,
13L, 1L, 10L, 18L, 12L, 21L, 14L, 2L, 7L, 17L, 16L, 25L, 22L,
3L, 8L, 15L, 18L, 6L, 24L, 7L, 11L), .Label = c("Bush, Jeb",
"Carson, Benjamin S.", "Christie, Christopher J.", "Clinton, Hillary Rodham",
"Cruz, Rafael Edward 'Ted'", "Fiorina, Carly", "Gilmore, James S III",
"Graham, Lindsey O.", "Huckabee, Mike", "Jindal, Bobby", "Johnson, Gary",
"Kasich, John R.", "Lessig, Lawrence", "McMullin, Evan", "O'Malley, Martin Joseph",
"Pataki, George E.", "Paul, Rand", "Perry, James R. (Rick)",
"Rubio, Marco", "Sanders, Bernard", "Santorum, Richard J.", "Stein, Jill",
"Trump, Donald J.", "Walker, Scott", "Webb, James Henry Jr."), class = "factor"),
economic_area = structure(c(2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L,
3L, 3L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 2L,
2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 1L, 2L, 3L,
1L, 1L, 3L, 2L, 3L, 2L, 1L, 2L, 2L, 3L, 1L, 3L, 2L, 1L, 3L,
3L, 1L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 2L, 1L
), .Label = c("Poorer areas", "Middle income areas", "Upper class areas"
), class = "factor"), sum = c(125925526.85, 16410009.07,
5622760.67, 4233067.76, 2551429, 1985456.25, 1626777.31,
1154448.33, 1104488.41, 860788, 621133.75, 587637, 566709.69,
436575.16, 358550.75, 271350.07, 227328.63, 218724.23, 211130.5,
211075, 197447.84, 195618, 192773, 191260.1, 167872.43, 161952.1,
141996.17, 121297.5, 92806, 80349.55, 70725.29, 66755, 63672,
62400, 58971, 48382.4, 48291.56, 44000, 43426.5, 38671.22,
37890, 29900, 28964, 21753, 20750, 19626.42, 16655.1, 15980.75,
15021, 12550, 12250, 12150, 9900, 8434.5, 6092, 5800, 5450,
4425.66, 4050, 2936, 2700, 2700, 2700, 1750, 1000, 1000,
600, 575)), row.names = c(NA, -68L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "cand_nm", drop = TRUE, .Names = c("cand_nm",
"economic_area", "sum"), indices = list(c(4L, 9L, 48L), c(13L,
25L, 54L), c(11L, 19L, 60L), c(0L, 1L, 20L), c(7L, 14L, 38L),
c(17L, 27L, 64L), c(55L, 66L), c(15L, 32L, 61L), c(28L, 42L,
44L), c(45L, 49L), c(24L, 30L, 67L), c(10L, 18L, 51L), c(36L,
47L), c(43L, 53L), c(21L, 40L, 62L), c(16L, 33L, 57L), c(26L,
29L, 56L), c(50L, 63L), c(6L, 12L, 31L), c(2L, 5L, 35L),
c(46L, 52L), c(23L, 34L, 59L), c(3L, 8L, 39L), c(22L, 37L,
65L), c(41L, 58L)), group_sizes = c(3L, 3L, 3L, 3L, 3L, 3L,
2L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 3L,
3L, 3L, 2L), biggest_group_size = 3L, labels = structure(list(
cand_nm = structure(1:25, .Label = c("Bush, Jeb", "Carson, Benjamin S.",
"Christie, Christopher J.", "Clinton, Hillary Rodham", "Cruz, Rafael Edward 'Ted'",
"Fiorina, Carly", "Gilmore, James S III", "Graham, Lindsey O.",
"Huckabee, Mike", "Jindal, Bobby", "Johnson, Gary", "Kasich, John R.",
"Lessig, Lawrence", "McMullin, Evan", "O'Malley, Martin Joseph",
"Pataki, George E.", "Paul, Rand", "Perry, James R. (Rick)",
"Rubio, Marco", "Sanders, Bernard", "Santorum, Richard J.",
"Stein, Jill", "Trump, Donald J.", "Walker, Scott", "Webb, James Henry Jr."
), class = "factor")), row.names = c(NA, -25L), class = "data.frame", vars = "cand_nm", drop = TRUE, .Names = "cand_nm"))
I would basically like to combine these three graphs into one extended graph grid / page:
1)
2)
3)

Variable label position in ggplot line chart

I have the following data frame summary created with dplyr
structure(list(maxrep = c(7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L,
11L, 12L, 12L, 13L, 13L, 14L, 14L, 15L, 15L, 16L, 16L, 17L, 17L,
18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 24L,
24L, 26L, 26L), div = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Premier Division",
"Second Division"), class = "factor"), freq = c(1L, 10L, 4L,
39L, 26L, 89L, 73L, 146L, 107L, 162L, 117L, 133L, 121L, 125L,
116L, 91L, 110L, 65L, 95L, 43L, 75L, 38L, 43L, 24L, 38L, 16L,
36L, 5L, 15L, 2L, 9L, 7L, 9L, 1L, 3L, 3L, 2L, 1L)), .Names = c("maxrep",
"div", "freq"), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -38L))
My intention is to use ggplot2 to plot line graphs of 2 lines with different colour with text labels for each value.
What I did was
ggplot(df, aes(x=maxrep, y=freq, colour=div)) +
geom_line() +
geom_text(aes(label=freq), vjust=-.5)
The result was
Now my question: All the labels in the chart are above the points in respective lines. I want to have the labels for the different colours to be in different relative position, e.g. labels for cyan above the line, and labels for red below the line (i.e. variable vjust). Is there a way to do that?
Also, is there a way to get read of the letter a in the colour legend on the right?
What about plotting the lines separately wich differing vjust values? You can get rid of a in the legend setting show_guide = FALSE.
ggplot(df, aes(x=maxrep, y=freq, colour=div, label = freq)) +
geom_line() +
geom_text(data = df[df$div == "Second Division",], vjust=2, show_guide = FALSE) + geom_text(data = df[df$div == "Premier Division",], vjust=-2, show_guide = FALSE)
Which returns:
Create a new variable in the data.frame holding the vjust adjustment parameter:
df$pos <- c(2, -2)[(df$div == "Premier Division")+1]
And you could call vjust inside aes with the new pos vector:
ggplot(df, aes(x=maxrep, y=freq, colour=div)) +
geom_line() +
geom_text(aes(label=freq, vjust=pos))

Resources