I have been using the following guide to export plots made with ggplotto pdf: plot fonts guide
It raises the issue at the bottom of the post of some fonts not appearing as they should, which happens in my example below. The text in font Bauhaus 93 appears correctly whilst the text in Calibri is displayed incorrectly.
Has anyone found a way to resolve this issue?
library(ggplot2)
library(plyr)
library(grid)
library(gridExtra)
library(extrafont)
data1<-as.data.frame(1:5)
data1[,2]<-as.data.frame(c(1,3,5,7,9))
data1[,3]<-as.data.frame(c(2,4,6,8,10))
colnames(data1)<-c("x","y1","y2")
ggplot(data1, aes(x=x)) +
geom_line(aes(y = y1, colour = "Taux selon DEF"), size=0.61, colour="black") +
geom_line(aes(y = y2, colour = "Taux selon EC"), size=0.61, colour="black", linetype="dashed") +
xlab("X axis lab") + ylab("Y axis lab)") +
annotate("text", x=1, y=4, label="Some text here", size=2, family="Bauhaus 93") +
annotate("text", x=4, y=1, label="More text here", size=2, family="Calibri") +
theme_bw() + theme(legend.title = element_blank(),
legend.key = element_rect(fill=NA),
panel.border = element_blank(),
axis.line = element_line(colour="black", size=0.25),
axis.ticks = element_line(size=0.25),
panel.grid.major = element_line(colour = "grey80", size=0.25),
panel.grid.minor = element_line(colour = "grey80", size=0.25),
axis.text.x = element_text(size=5.5 , lineheight=0.9, hjust=0.5, family="Bauhaus 93"),
axis.text.y = element_text(size=5.5 , lineheight=0.9, vjust=0.5, family="Calibri"),
axis.title.y = element_text(size=6.1, angle=0, vjust=0.975, face="bold", family="Calibri"),
axis.title.x = element_text(size=6.1, angle=0, vjust=-0.20, face="bold", family="Calibri")) +
scale_x_continuous(expand = c(0, 0), limits=c(0,5)) +
scale_y_continuous(expand = c(0, 0), limits=c(0,10)) +
ggtitle("Title") +
ggsave("Test.pdf", width=7, height=5)
Sys.setenv(R_GSCMD = "C:/Program Files (x86)/PDF24/gs/bin/gswin32.exe")
embed_fonts("Test.pdf")
Try adding device=cairo_pdf to the ggsave() call. This appears to solve the problem for me. This way, it's no longer necessary to use embed_fonts().
See mgaudet's comment here: https://github.com/wch/extrafont/issues/8
Related
I want to add a latex expression in my legend like red-colored. I tried the following code, but I could not do that. How do I manage this issue? Also, I got a waring about this code. How do I remove that?
library(ggplot2)
ggplot(C, aes(sigma, value)) +
geom_line(aes(linetype=name)) +
scale_linetype_manual(values=c("solid", "twodash", "dotted")) +
scale_linetype_discrete(labels = c(expression("X_A = 1 \\& X_B =0"),
expression("X_A = 3 \\& X_B =0"),
expression("X_A = 5 \\& X_B =0"))) +
theme_minimal() +
xlab("standard diviation") +
ylab("rejection rate") +
theme_classic() +
theme(legend.position = c(0.15, 0.85),
legend.background = element_rect(fill="white",
size=0.5,
linetype="solid",
colour ="gray"),
legend.spacing.y = unit(0, "pt"),
text = element_text(size=20),
legend.text = element_text(size=20),
axis.title=element_text(size=20),
legend.title = element_blank(),
panel.grid.major.y = element_line(),
panel.grid.minor.y = element_line())
Scale for 'linetype' is already present. Adding another scale for 'linetype',
which will replace the existing scale.
I made a chart in which geom_bar() and geom_point() were combined. This worked well. But now I have two more questions about the appearance:
(1) Is there a way to insert "ticks" inside the graphic (like in picture 2)?
(2) Some of the values are the same, which causes the points to overlap (& some are therefore no longer visible). Is there a way to offset the points?
Thank you, here my code:
ggplot() +
geom_bar(data=data1, aes(Question, HG, fill = yes_share), stat = "identity") +
geom_point(data=data2, aes(Question, yes_share, color = factor(party), size=3)) +
coord_flip() +
scale_color_manual(values=c("#efc100","#ff9100", "#61b5a5","#3a8bc1", "#a0a000", "#66cc00","#825000", "#726F44","#dd0e0e","#0a7228", "#a7c1a8")) +
scale_fill_manual(values=c(col1, col2)) +
theme_bw() +
xlab("") + ylab("") +
theme(panel.grid.major.x = element_line(size=.1, color="black") ,
panel.grid.minor.x = element_blank() ,
panel.grid.major.y = element_blank() ,
panel.grid.minor.y = element_blank() ,
panel.border = element_blank(),
legend.position="bottom",
legend.text = element_text(size = 10),
axis.text = element_text(size = 10))
How it looks
How it should look
I'm a beginner using R and I'm creating a ggplot-geom_bar (stack) to demonstrate diferences in percentage of compounds. However I have more than 11 colours and I cannot change the fill colours
I'm using the following code:
vermelha_bio_stack <- ggplot() +
geom_bar(data=vermelha_pom,
aes(y=values, x=month,fill= factor(compunds)),
stat="identity") +
xlab("") +
ylab("Percentages") +
scale_fill_discrete(name = "Compounds") +
scale_y_continuous(limits = c(0,101), expand=c(0,0)) +
theme_classic() +
theme(
text=element_text(family = "sans"),
axis.ticks.x = element_blank(),
axis.line.x.bottom = element_line (color = "black"),
axis.line.y.left = element_line(color = "black"),
panel.background = element_rect(fill = "white")) +
ggtitle("Lagoa Vermelha") +
theme(plot.title = element_text(color="black", size=14, face="bold", hjust = 0.5))+
coord_flip()
and I got the following plot
but I want colours like the following image:
I am trying to change one of the lines in my ggplot from dash to dotted but I fail, the function scale_linetype_manual doesn't do the work. Can anyone help me to solve this problem?
ggplot(d, aes(a,value)) +
geom_line(aes(color = series), size=2)+
scale_y_continuous(breaks=seq(-2.5, 2.5, 2.5)) +
coord_cartesian(ylim=c(-2.5, 2.5))+
scale_x_continuous(breaks=seq(-200, 2000, 1000)) +
scale_color_manual(values=c("#E69F00","#56B4E9", "#56B4E9")) +
scale_linetype_manual(values=c("twodash", "dotted", "dotted")) +
theme(legend.direction = 'vertical',
legend.position = 'right',
legend.key = element_rect(size = 7),
legend.key.size = unit(3, 'lines'))+
theme(panel.grid.major = element_blank(), text = element_text(size=30),
panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour =
"black"))+
geom_vline(xintercept=c(0), linetype="dotted", size=1.5)+
geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
color="gray55",
alpha=0.4,
inherit.aes = FALSE)+
labs(x = "time [ms]",
y = "Amplitude [µV]",
color = "")
The 'problem' with your plot was that you didn't map the aesthetic linetyppe
to a variable. Hence, your call to scale_linetype_manual had no effect. You should change your code to be
ggplot(d, aes(a,value)) +
geom_line(aes(color = series, linetype = series), size = 2) +
...
I have the following plot:
ggplot(proba[108:140,], aes(c,four, color="a1")) +
geom_line(linetype=1, size=0.3) +
scale_x_continuous(breaks=seq(110,140,5)) +
theme_bw() +
theme(axis.line = element_line(colour = "black", size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank()) +
theme(axis.text.x = element_text(angle = 0, hjust = +0.5, size=6,color="black")) +
theme(axis.text.y = element_text(angle = 0, hjust = -100, size=6, color="black")) +
theme(axis.ticks=element_line(colour="black",size=0.25)) +
xlab("\nTime-steps")+ylab("Proportion correct\n") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8)) +
geom_line(aes(c,three, color="a2"), size=0.2, linetype=2) +
geom_line(aes(c,one, color="a3"),linetype=3, size=0.8) +
geom_line(aes(c,two, color="a4"), linetype=1, size=0.6) +
scale_color_manual(values=c("a1"="red3", "a2"="red3","a3"="blue3","a4"="blue3")) +
theme(legend.title = element_text(colour="black", size=7)) +
theme(legend.position="bottom" ,
legend.direction="horizontal",
legend.title=theme_blank()) +
theme(legend.text=theme_text(size=7),
legend.background=theme_blank(),
legend.key=theme_blank())
The four lines on the plot are displayed in different linetypes, however the legend does not show these different linetypes only the different colours. I am obviously missing a very simple argument in theme() but I cannot figure out what it is. Maybe I need to specify the linetypes again in scale_colour_manual()?
Here's the data: https://dl.dropboxusercontent.com/u/22681355/proba.csv
proba<-read.csv("proba.csv",head=T)
Here, this is the correct way to do this in ggplot2:
proba <- read.csv("~/Downloads/proba.csv")
proba1 <- proba[108:140,]
proba1 <- melt(proba1,id.vars = 1:2)
ggplot(proba1,aes(x = c,y = value,colour = variable,linetype = variable,size = variable)) +
geom_line() +
scale_x_continuous(breaks=seq(110,140,5)) +
scale_colour_manual(values=c("blue3","blue3","red3","red3")) +
scale_linetype_manual(values = c(2,1,3,1)) +
scale_size_manual(values = c(0.2,0.3,0.8,0.6)) +
xlab("\nTime-steps") +
ylab("Proportion correct\n") +
theme_bw() +
theme(axis.text=element_text(size=6),
axis.title=element_text(size=8),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="bottom" ,
legend.direction="horizontal",
legend.title=element_blank(),
legend.text=element_text(size=7),
legend.background=element_blank(),
legend.key=element_blank())
Try to keep all your theme adjustments in one call to theme, otherwise things get pretty messy. Also, you had some calls to theme_* that should have been element_*.