I'm still pretty much a novice with R and ggplot. I have the following code
library(ggplot2)
library(dplyr)
library(tidyr)
maxDate <- "2020-07-07"
my_dates <- function(d) {
seq( d[1] + (wday(maxDate) - wday(d[1])+1) %% 7, d[2] + 6, by = "week")
}
stateWeekly <- #structure at https://pastebin.com/jT8WV4dy
endpoints <- stateWeekly %>%
group_by(state) %>%
filter(weekStarting == max(weekStarting)) %>%
select(weekStarting, posRate, state, cumRate, posRateChange) %>%
ungroup()
g <- stateWeekly %>% ggplot(aes(x = as.Date(weekStarting))) +
geom_col(aes(y=100*dailyTest), size=0.75, color="darkblue", fill="white") +
geom_line(aes(y=cumRate), size = 0.75, color="red") +
geom_line(aes(y=posRate), size = 0.75, color="forestgreen") +
geom_point(data = endpoints,size = 1.5,shape = 21,
aes(y = cumRate), color = "red", fill = "red", show.legend = FALSE) +
geom_label(data=endpoints, aes(label=paste(round(cumRate,1),"%",sep=""),
x=as.Date("2020-04-07", format="%Y-%m-%d"), y = 80),
color="red",
show.legend = FALSE,
nudge_y = 12) +
geom_label(data=endpoints, aes(label=paste(round(posRateChange,1),"%",sep=""),
x=as.Date("2020-04-28", format="%Y-%m-%d"), y = 80),
color="forestgreen",
show.legend = FALSE,
nudge_y = 12) +
scale_y_continuous(name = "Cum Test Positivity Rate",
sec.axis = sec_axis(~./100, name="Weekly % of Pop Tested")) +
scale_x_date(breaks = my_dates, date_labels = "%b %d") +
labs(x = "Week Beginning") +
#title = "COVID-19 Testing",
#subtitle = paste("Data as of", format(maxDate, "%A, %B %e, %y")),
#caption = "HQ AFMC/A9A \n Data: The COVID Tracking Project (https://covidtracking.com)") +
theme(plot.title = element_text(size = rel(1), face = "bold"),
plot.subtitle = element_text(size = rel(0.7)),
plot.caption = element_text(size = rel(1)),
axis.text.y = element_text(color='red'),
axis.title.y = element_text(color="red"),
axis.text.y.right = element_text(color="blue"),
axis.title.y.right = element_text(color="blue"),
axis.text.x = element_text(angle = 45,hjust = 1),
strip.background =element_rect(fill="white"),
strip.text = element_text(colour = 'blue')) +
#coord_cartesian(ylim=c(0,90)) +
facet_wrap(~ state)
print(g)
Which produces this chart
Georgia has obviously been screwing with their COVID data (again) so nevermind the negative testing :)
What I'd like to do is scale the secondary axis so that the testing rate lines aren't so squished...they're very small numbers but I'd like to be able to see more differentiation. Any guidance on how to achieve that would be most appreciated.
Edit:
One suggestion below was to change facet_wrap(~ state) to facet_wrap(~ state, scales='free') Doing so changes the chart only slightly
I can fix the label anchors, but this really didn't offer the level of differentiation in the line plots I was hoping for.
A second suggestion was the change sec.axis = sec_axis(~./100, name="Weekly % of Pop Tested")) to sec.axis = sec_axis(~./1000, name="Weekly % of Pop Tested"))
As far as I can tell, that does nothing to the actual plot and just changes the axis markings:
Finally, I've been struggling to implement the solution found here from Dag Hjermann. My secondary axis is the Weekly % of Population Tested, which is represented in the geom_col. A reasonable range for that is 0-1.1. The primary axis is the line plots, test positivity rates, which vary from 0-30. So if I follow that solution, I should add
ylim.prim <- c(0, 30)
ylim.sec <- c(0, 1.1)
b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])
and then change the plot code to read
geom_col(aes(y=a + 100*dailyTest*b), size=0.75, color="darkblue", fill="white")
and the secondary axis to
sec.axis = sec_axis(~ (. -a)/(b*100), name="Weekly % of Pop Tested"))
Doing so produces the following
which is clearly not right.
At the risk of sounding really dumb here, is the issue at least somewhat due to the line plots (what I want to scale) being on the primary axis?
Maybe use permille instead of percentage.
scale_y_continuous(name = "Cum Test Positivity Rate",
sec.axis = sec_axis(~./1000, name="Weekly ‰ of Pop Tested"))
Related
I want to display both an annual value (school hours) and its cumulative (total school hours) in the same graph. This works, so now I want to tidy-up the legend a little bit. I want to drop the three groups associated with the cumulative values and only report a custom label for each state I use. Thus, the legend should only read "NW, G8", "NW, G9", and "CA", next to the colour they are associated with.
I have found other solutions to this problem which no longer seem to work with the current ggplot version (at least I believe this to be a version-issue, maybe I made another mistake):
https://community.rstudio.com/t/how-keep-aesthetic-mapping-but-remove-a-specific-item-from-legend-with-ggplot/52818/3
-> trying to replicate the solution with the provided code results in a gray barplot "c" and not a blue bar plot for me.
Remove legend entries for some factors levels
-> again, similar solution, but if I update my scale_color_manual to the following alternative, I again have gray lines for my cumulative values
scale_color_manual(breaks = c("hours_nw_G8", "hours_nw_G9", "hours_ca"),
values = c("#073B4C", "#118AB2", "#FFD166", "#073B4C", "#118AB2", "#FFD166")) +
Code:
require(tidyverse)
## school hours: comparison of US state (CA) and DE State (NW)
df_hours <- data.frame(year=c(1:13),
hours_nw_G8=c(21.5,22.5,25.5,26.5,31.5,31.5,32.5,32.5,33.5,34,34,34,NA),
hours_nw_G9=c(21.5,22.5,25.5,26.5,28,29,30,30,31,31,31.5,29.5,29.5),
hours_ca=c(840,840,840,900,900,900,900,900,1080,1080,1080,1080,NA)
)
df_hours$hours_nw_G8 <- df_hours$hours_nw_G8 * 38 * 0.75 # scaling by #weeks and accounting for German school hour
df_hours$hours_nw_G9 <- df_hours$hours_nw_G9 * 38 * 0.75
# cumulate
df_hours$c_hours_nw_G8 <- cumsum(df_hours$hours_nw_G8) / 8 # cummulate and divide by scaling factor
df_hours$c_hours_nw_G9 <- cumsum(df_hours$hours_nw_G9) / 8
df_hours$c_hours_ca <- cumsum(df_hours$hours_ca) / 8
# reshape & dummy for cumulative
df_hours <- gather(df_hours, state, hours, hours_nw_G8:c_hours_ca, factor_key=TRUE)
df_hours$cu <- c(rep("annual",13*3),rep("cumulative",13*3))
# Figure
ggplot(df_hours, aes(x=year)) +
geom_line(aes(y=hours, color=state, linetype=cu), linewidth = 1) +
scale_y_continuous(limits = c(0,1500), expand = c(0,0),
breaks = c(0,250,500,750,1000,1250,1500),
name="Hours (annual)",
sec.axis = sec_axis(~ .*8, name = "Hours (cummulative)",
breaks = c(0,2000,4000,6000,8000,10000,12000))
) +
scale_x_continuous(limits = c(0.5,13.5), expand = c(0,0), breaks = c(1:13)) +
labs(x="School Year", y="Hours") +
theme_tufte() +
scale_color_manual(values = c("#073B4C", "#118AB2", "#FFD166", "#073B4C", "#118AB2", "#FFD166")) +
theme(axis.line = element_line(linewidth = 0.75), text = element_text(size = 10, color = "black"),
legend.position = c(.3, .9), legend.title = element_blank()) +
guides(linetype = F)
One solution could be using some stringr function to modify the strings:
library(ggthemes)
library(tidyverse)
df_hours %>%
mutate(state_label = str_remove(state, "c_hours_|hours_"),
state_label = str_to_upper(state_label),
state_label = str_replace(state_label, "_", ", ")) %>%
ggplot(aes(x=year)) +
geom_line(aes(y=hours, color=state_label, linetype=cu), linewidth = 1) +
scale_y_continuous(limits = c(0,1500), expand = c(0,0),
breaks = c(0,250,500,750,1000,1250,1500),
name="Hours (annual)",
sec.axis = sec_axis(~ .*8, name = "Hours (cummulative)",
breaks = c(0,2000,4000,6000,8000,10000,12000))
) +
scale_x_continuous(limits = c(0.5,13.5), expand = c(0,0), breaks = c(1:13)) +
labs(x="School Year", y="Hours") +
theme_tufte() +
scale_color_manual(values = c("#073B4C", "#118AB2", "#FFD166", "#073B4C", "#118AB2", "#FFD166")) +
theme(axis.line = element_line(0.75), text = element_text(size = 10, color = "black"),
legend.position = c(.3, .9), legend.title = element_blank()) +
guides(linetype = F)
I have a gganimate sketch in R and I would like to have the percentages of my bar chart appear as labels.
But for some bizarre reason, I am getting seemingly random colours in place of the labels that I'm requesting.
If I run the ggplot part without animating then it's a mess (as it should be), but it's obvious that the percentages are appearing correctly.
Any ideas? The colour codes don't correspond to the colours of the bars which I have chosen separately. The codes displayed also cycle through about half a dozen different codes, at a rate different to the frame rate that I selected. And while the bars are the same height (they grow until they reach the chosen height displayed in the animation) then they display the same code until they stop and it gets frozen.
Code snippet:
df_new <- data.frame(index, rate, year, colour)
df_new$rate_label <- ifelse(round(df_new$rate, 1) %% 1 == 0,
paste0(round(df_new$rate, 1), ".0%"), paste0(round(df_new$rate, 1), "%"))
p <- ggplot(df_new, aes(x = year, y = rate, fill = year)) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = colour) +
#geom_text(aes(y = rate, label = paste0(rate, "%")), vjust = -0.7) +
geom_shadowtext(aes(y = rate, label = rate_label),
bg.colour='white',
colour = 'black',
size = 9,
fontface = "bold",
vjust = -0.7,
alpha = 1
) +
coord_cartesian(clip = 'off') +
ggtitle("% population belonging to 'No religion', England and Wales census") +
theme_minimal() +
xlab("") + ylab("") +
theme(legend.position = "none") +
theme(plot.title = element_text(size = 18, face = "bold")) +
theme(axis.text = element_text(size = 14)) +
scale_y_continuous(limits = c(0, 45), breaks = 10*(0:4))
p
p <- p + transition_reveal(index) + view_follow(fixed_y = T)
animate(p, renderer = gifski_renderer(), nframes = 300, fps = frame_rate, height = 500, width = 800,
end_pause = 0)
anim_save("atheism.gif")
I think you have missed some delicate points about ggplot2. I will try my best to describe them to you. First of all, you need to enter the discrete values as factor or integer. So you can use as.factor() before plotting or just factor() in the aesthetic. Also, you should consider rounding the percentages as you wish. Here is an example:
set.seed(2023)
df_new <- data.frame(index=1:10, rate=runif(10), year=2001:2010, colour=1:10)
df_new$rate_label <- ifelse(round(df_new$rate, 1) %% 1 == 0,
paste0(round(df_new$rate, 1), ".0%"),
paste0(round(df_new$rate, 1), "%"))
The ggplot for this data is:
library(ggplot2)
p <- ggplot(df_new, aes(x = factor(year), y = rate, fill = factor(colour))) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(y = rate, label = paste0(round(rate,2), "%")), vjust = -0.7) +
coord_cartesian(clip = 'off') +
ggtitle("% population belonging to 'No religion', England and Wales census") +
theme_minimal() +
xlab("") + ylab("") +
theme(legend.position = "none",
plot.title = element_text(size = 18, face = "bold"),
axis.text = element_text(size = 14))
p
And you can combine all theme element in one theme() function (as did I). The output is:
And you can easily animate the plot using the following code:
library(gganimate)
p + transition_reveal(index)
And the output is as below:
Hope it helps.
So it was answered here although I don't know why the fix works.
For some reason, labels need to go into gganimate as factors
as.factor()
I just had to add the line:
df_new$rate_label <- as.factor(df_new$rate_label)
and it works fine.
How can please using ggplot2 package or whatever else package remove that "blank space " between months caused by absence of certain months on my x axis ? In other words to make the x axis looks equidistant and not having those "blank gapes".By the code is very normal ,it is about plotting certain values vs other column containing dates (not all the months are present in that date column ).
filtredplot1<-reactive({
req(res_mod())
dat<-res_mod()
dt<-dat[dat$M_Datum >= input$dateRange[1] & dat$M_Datum <= input$dateRange[2],]
dt[,5]<-as.Date(format(as.Date(dt[,5]), "%Y-%m-01"))
req(dt$M_Datum,dt$Yield)
dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
mydf=aggregate(Yield ~ M_Datum, dr, length)
req(mydf$M_Datum,mydf$Yield)
koka<-data.frame("M_Datum"=mydf$M_Datum,"Yiel"=mydf$Yield)
ggplot(koka, aes(x=factor(format(M_Datum, "%b %Y")), y=Yiel,group = 1)) +
geom_point(size=7,colour="#EF783D",shape=17) +
geom_line(color="#EF783D")+
scale_x_date(labels="%b %Y")
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
theme(axis.text.y.left = element_text(color = "#EF783D"),
axis.title.y.left = element_text(color = "#EF783D"))+
ylab("Wafer Quantity")+
xlab("")
})
The code below does not create the data.frames dr and mydf, your data preparation code is too complicated. The following is much simpler and works.
Also, you have the typo Yiel for Yield twice in your code. The first when creating koka and the second in aes().
suppressPackageStartupMessages({
library(shiny)
library(ggplot2)
})
dt <- data.frame(
M_Datum=c("2018-02-05","2018-02-15","2018-02-10","2018-02-13","2017-02-05",
"2017-02-15","2017-02-10","2017-02-23","2020-02-25","2012-02-15",
"2020-02-10","2020-02-13"),
Yield=c(4,47,18,10,22,50,70,120,150,400,60,78)
)
dt$M_Datum <- as.Date(dt$M_Datum)
req(dt$M_Datum, dt$Yield)
koka <- aggregate(Yield ~ M_Datum, dt, length)
ggplot(koka, aes(x = M_Datum, y = Yield, group = 1)) +
geom_point(size = 7, colour = "#EF783D", shape = 17) +
geom_line(color = "#EF783D") +
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
xlab("") +
ylab("Wafer Quantity") +
theme(
axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
axis.text.y.left = element_text(color = "#EF783D"),
axis.title.y.left = element_text(color = "#EF783D")
)
I'm trying to plot Alluvial Plots using ggplot. So far it went well until I want to try to clean the plot up.
As you can see on the plot, from left to right, the first stratum/column is the ID column then it follows by a column of labels: disease risk. What I want to achieve is in the out plot, instead of having the patient IDs zigzagging, I want them to be ordered by disease risk column, so that all the high risk IDs are all together on top, followed by low risk then the not filled ones. In this way it is much easier to see if there's any relations.
I have looked around for the arrange() and order() functions, they seem to do the trick for my actual input data but once I pass that data frame in ggplot, the output figure is still scrambled.
I thought of set the IDs to factor, then use levels=.... But this is not very smart if the patient ID keeps growing.
Is there a smarter way? please enlighten me. I have attached a link towards the sample data.
https://drive.google.com/file/d/16Pd8V3MCgEHmZEButVi2UjDiwZWklK-T/view?usp=sharing
My code to plot the graph :
library(tidyr)
library(ggplot2)
library(ggalluvial)
library(RColorBrewer)
# Define the number of colors you want
nb.cols <- 10
mycolor1 <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
mycolors <- c("Black")
#read the data
CLL3S.plusrec <- read.csv("xxxx.CSV", as.is = T)
CLL3S.plusrec$risk_by_DS <- factor(CLL3S.plusrec$risk_by_DS, levels = c("low_risk", "high_risk", "Not filled"))
CLL3S.plusrec$`Enriched response phenotype` <- factor(CLL3S.plusrec$`Enriched response phenotype`, levels = c("Live cells","Pre-dead", "TN & PDB", "PDB & Lenalidomide", "TN & STSVEN & Live cells","Mixed"))
#here I reorder the dataframe and it looks good
#but the output ggplot changes the order of ID in the output graph
OR <- with(CLL3S.plusrec, CLL3S.plusrec[order(risk_by_DS),])
d <-ggplot(OR, aes(y = count,
axis1= Patient.ID,
axis2= risk_by_DS,
axis3 = `Cluster assigned consensus`,
axis4 = `Cluster assigned single drug`,
axis5 = `Enriched response phenotype`
)) +
scale_x_discrete(limits = c("Patient ID","Disease Risk", "Consensus cluster", "Single-drug cluster", "Enriched drug response by Phenoptype")) +
geom_alluvium(aes(fill=`Cluster assigned consensus`)) +
geom_stratum(width = 1/3, fill = c(mycolor1[1:69],mycolor1[1:3],mycolor1[1:8],mycolor1[1:8],mycolor1[1:6]), color = "red") +
#geom_stratum() +
geom_text(stat = "stratum", aes(label = after_stat(stratum)), size=3) +
theme(axis.title.x = element_text(size = 15, face="bold"))+
theme(axis.title.y = element_text(size = 15, face="bold"))+
theme(axis.text.x = element_text(size = 10, face="bold")) +
theme(axis.text.y = element_text(size = 10, face="bold")) +
labs(fill = "Consensus clusters")+
guides(fill=guide_legend(override.aes = list(color=mycolors)))+
ggtitle("Patient flow between the Consensus clusters and Single-drug treated clusters",
"3S stimulated patients")
print(d)
Not sure if this is what you want, try formating the risk column in this way:
library(tidyr)
library(ggplot2)
library(ggalluvial)
library(RColorBrewer)
# Define the number of colors you want
nb.cols <- 10
mycolor1 <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)
mycolors <- c("Black")
#read the data
CLL3S.plusrec <- read.csv("test data.CSV", as.is = T)
CLL3S.plusrec$risk_by_DS <- factor(CLL3S.plusrec$risk_by_DS,
levels = c("high_risk","low_risk","Not filled"),ordered = T)
CLL3S.plusrec$Enriched.response.phenotype <- factor(CLL3S.plusrec$Enriched.response.phenotype, levels = c("Live cells","Pre-dead", "TN & PDB", "PDB & Lenalidomide", "TN & STSVEN & Live cells","Mixed"))
#here I reorder the dataframe and it looks good
#but the output ggplot changes the order of ID in the output graph
OR <- with(CLL3S.plusrec, CLL3S.plusrec[order(risk_by_DS),])
ggplot(OR, aes(y = count,
axis1= reorder(Patient.ID,risk_by_DS),
axis2= risk_by_DS,
axis3 = reorder(Cluster.assigned.consensus,risk_by_DS),
axis4 = reorder(Cluster.assigned.single.drug,risk_by_DS),
axis5 = reorder(Enriched.response.phenotype,risk_by_DS)
)) +
scale_x_discrete(limits = c("Patient ID","Disease Risk", "Consensus cluster", "Single-drug cluster", "Enriched drug response by Phenoptype")) +
geom_alluvium(aes(fill=Cluster.assigned.consensus)) +
geom_stratum(width = 1/3, fill = c(mycolor1[1:69],mycolor1[1:3],mycolor1[1:8],mycolor1[1:8],mycolor1[1:6]), color = "red") +
#geom_stratum() +
geom_text(stat = "stratum", aes(label = after_stat(stratum)), size=3) +
theme(axis.title.x = element_text(size = 15, face="bold"))+
theme(axis.title.y = element_text(size = 15, face="bold"))+
theme(axis.text.x = element_text(size = 10, face="bold")) +
theme(axis.text.y = element_text(size = 10, face="bold")) +
labs(fill = "Consensus clusters")+
guides(fill=guide_legend(override.aes = list(color=mycolors)))+
ggtitle("Patient flow between the Consensus clusters and Single-drug treated clusters",
"3S stimulated patients")
Output:
Also in my read.csv() the quotes got off and dots are in the variables. That is why your original quoted variables now have dots. Maybe an issue from reading.
Update:
#Update
OR <- with(CLL3S.plusrec, CLL3S.plusrec[order(risk_by_DS),])
OR <- OR[order(OR$risk_by_DS,OR$Patient.ID),]
OR$Patient.ID <- factor(OR$Patient.ID,levels = unique(OR$Patient.ID),ordered = T)
#Plot
ggplot(OR, aes(y = count,
axis1= reorder(Patient.ID,risk_by_DS),
axis2= risk_by_DS,
axis3 = reorder(Cluster.assigned.consensus,risk_by_DS),
axis4 = reorder(Cluster.assigned.single.drug,risk_by_DS),
axis5 = reorder(Enriched.response.phenotype,risk_by_DS)
)) +
scale_x_discrete(limits = c("Patient ID","Disease Risk", "Consensus cluster", "Single-drug cluster", "Enriched drug response by Phenoptype")) +
geom_alluvium(aes(fill=Cluster.assigned.consensus)) +
geom_stratum(width = 1/3, fill = c(mycolor1[1:69],mycolor1[1:3],mycolor1[1:8],mycolor1[1:8],mycolor1[1:6]), color = "red") +
#geom_stratum() +
geom_text(stat = "stratum", aes(label = after_stat(stratum)), size=3) +
theme(axis.title.x = element_text(size = 15, face="bold"))+
theme(axis.title.y = element_text(size = 15, face="bold"))+
theme(axis.text.x = element_text(size = 10, face="bold")) +
theme(axis.text.y = element_text(size = 10, face="bold")) +
labs(fill = "Consensus clusters")+
guides(fill=guide_legend(override.aes = list(color=mycolors)))+
ggtitle("Patient flow between the Consensus clusters and Single-drug treated clusters",
"3S stimulated patients")
Output:
I have this plot
library(dplyr)
library(ggplot2)
indexYear <- as.numeric(2000:2010)
lDLatIndex <- rep.int(4,11)
LDLoneYear <- c(rep.int(3,5), rep.int(2,6))
hba1catIndex <- c(rep.int(8,6), rep.int(7.5,5))
hba1coneYear <- rep.int(7,11)
LDLeffect <- data.frame(indexYear, lDLatIndex, hba1catIndex, hba1coneYear)
LDLeffect %>%
ggplot(., aes(x = indexYear))+
geom_line(aes(y = lDLatIndex, colour=rgb(237/255, 115/255,116/255)), linetype = "solid" , size = 2)+
theme_classic()+
theme(legend.position = "bottom")+
ylab("mean LDL cholesterol (mmol/l) ")+
xlab("Calendar year")+
theme(axis.title = element_text(size = 17, face="bold"), axis.text = element_text(size = 17, face = "bold"))+
scale_x_continuous(breaks = seq(2000,2015, by=1),labels = c(2000,rep("",4),2005,rep("",4), 2010, rep("",4),2015))+
scale_y_continuous(sec.axis = sec_axis(~(.-2.15)*10.929, name = "mean HbA1c (%) "))+
geom_line(aes(y = LDLoneYear, colour=rgb(237/255, 115/255,116/255)), linetype = "dashed" , size = 2)+
geom_line(aes(y = hba1coneYear, colour=rgb(152/255, 201/255,139/255)), linetype = "twodash" , size = 2)+
geom_line(aes(y = hba1catIndex, colour=rgb(152/255, 201/255,139/255)), linetype = "F1" , size = 2)
I know that usually, the best option is to supply data in long format for ggplot, but I couldn't get it to work. The plot above produces a strange legend that I cannot understand how got there.
I see that the names to the legend added are from the colour definitions.
What I want to make is legends that show the colour and linetype and name for each variable plotted, preferably with the option of adding custom names. I looked through a lot of pages with suggestions, but most makes use of long format which I cannot figure out because I wanted different linetypes and colours by two and two. The rest couldn't help me address this strange expression in the labelling.
Would below proposal go into right direction? Main points are: using "melt" from reshape2 for bringing data in ggplot-friendly shape. And with scale_linetype_manual and scale_colour_manual I'm explicitly providing colours and line types.
library(dplyr)
library(ggplot2)
library(reshape2) ## for "melt"
indexYear <- as.numeric(2000:2010)
lDLatIndex <- rep.int(4,11)
LDLoneYear <- c(rep.int(3,5), rep.int(2,6))
hba1catIndex <- c(rep.int(8,6), rep.int(7.5,5))
hba1coneYear <- rep.int(7,11)
LDLeffect <- data.frame(indexYear, lDLatIndex, hba1catIndex, hba1coneYear, LDLoneYear)
melted_df <- melt(LDLeffect, id.vars="indexYear", measure.vars=c("lDLatIndex", "hba1catIndex", "hba1coneYear", "LDLoneYear"))
ggplot(melted_df, aes(x=indexYear, value, colour=variable)) +
geom_line(aes(linetype=variable), size = 2) +
scale_linetype_manual(values=c("F1", "twodash", "solid", "dashed")) +
scale_colour_manual(values=c(rgb(237/255, 115/255,116/255), rgb(237/255, 115/255,116/255), rgb(152/255, 201/255,139/255), rgb(152/255, 201/255,139/255))) +
theme_classic() +
theme(legend.position = "bottom")+
ylab("mean LDL cholesterol (mmol/l)")+
xlab("Calendar year")+
theme(axis.title = element_text(size = 17, face="bold"), axis.text = element_text(size = 17, face = "bold"))+
scale_x_continuous(breaks = seq(2000,2015, by=1),labels = c(2000,rep("",4),2005,rep("",4), 2010, rep("",4),2015))+
scale_y_continuous(sec.axis = sec_axis(~(.-2.15)*10.929, name = "mean HbA1c (%)"))