I have a data.frame df as follows,
df <- data.frame(x = 0:50, y = rnorm(n = 51, mean = 25, sd = 74))
I want to annotate the axis ticks outside the plot in ggplot2. Following this question, I am able to do it as follows,
library(ggplot2)
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(12, 28), colour = "red") +
scale_x_continuous(breaks = c(0,12, 25,28,50), labels = c("0","Point 1", "25","Point 2","50")) +
theme(axis.text.x = element_text(color = c("black","red", "black", "red", "black")),
axis.ticks.x = element_line(color = c("black","red", "black", "red", "black"),
size = c(.5,1,.5,1,.5)))
However, when the points are close, the annotations overlap.
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(30, 28), colour = "red") +
scale_x_continuous(breaks = c(0, 25,28, 30, 50), labels = c("0","25","Point 1", "Point 2","50")) +
theme(axis.text.x = element_text(color = c("black","black", "red", "red", "black")),
axis.ticks.x = element_line(color = c("black","black", "red", "red", "black"),
size = c(.5,.5,1,1,.5)))
How to avoid this and get a desired plot as follows in ggplot2?
What about turning them using angle so they don't overlap.
ggplot(df, aes(x = x, y = y)) +
geom_line() +
geom_vline(xintercept = c(30, 28), colour = "red") +
scale_x_continuous(breaks = c(0, 25,28, 30, 50), labels = c("0","25","Point 1", "Point 2","50")) +
theme(axis.text.x = element_text(color = c("black","black", "red", "red", "black"), angle = 60, hjust = 1),
axis.ticks.x = element_line(color = c("black","black", "red", "red", "black"),
size = c(.5,.5,1,1,.5)))
Related
I have some batch test data which was performed under different mixing conditions.
While I have managed to group the data, I am not sure how to define different aethitics for the geom_smooth lines.
M3<- ggplot(subset(VFA2, VFA %in% "HPr"), aes(x = Time, y = value, group = mix, shape = Test, colour = Test))+geom_point(size = 6, colour = "purple4")+
labs(x = "Anaerobic Time (h)\n Continuous Mixing", y = "HPr Concentration\n(mg HPr/L)")+theme(panel.background = element_rect(fill = "White", colour = "grey"),panel.grid.major = element_line(color = 'grey'), legend.position="bottom", text= element_text(size = 28, family = "Arial"))+
scale_x_discrete(breaks = factor(VFA2$Time), expand = c(-0.25,2))+scale_y_continuous(limits = c(-0.25, 60),breaks = c(0, 10,20, 30, 40, 50, 60))+[![enter image description here][1]][1] scale_shape_manual(values = c(0,1,2,5,7,13,14,9))
M3<- M3 + geom_smooth(method= "gam", formula = y~poly(x,4), se = F, colour = "black", linetype = "dashed")
Is this what you are looking for :
M3<- ggplot(subset(VFA2, VFA %in% "HPr"), aes(x = Time, y = value, group = mix, shape = Test))+
geom_point(size = 6, colour = "purple4")+
geom_smooth(aes(colour=mix), method= "gam", formula = y~poly(x,4), se = F, linetype = "dashed")+
labs(x = "Anaerobic Time (h)\n Continuous Mixing", y = "HPr Concentration\n(mg HPr/L)")+
theme(panel.background = element_rect(fill = "White", colour = "grey"),panel.grid.major = element_line(color = 'grey'), legend.position="bottom", text= element_text(size = 28, family = "Arial"))+
scale_x_discrete(breaks = factor(VFA2$Time), expand = c(-0.25,2))+
scale_y_continuous(limits = c(-0.25, 60),breaks = c(0, 10,20, 30, 40, 50, 60))+
scale_shape_manual(values = c(0,1,2,5,7,13,14,9))+
scale_color_manual(values = c("black", "red"))
Need to display the x-axis levels in neatly way without affecting the actual point numbers in the final output. As currently, I am getting x-axis in closely spaced which looks not good while I am showing in powerpoint
library("readxl")
my_data <-read_excel("central_high.xlsx") # Input file
str(my_data)
my_data = as.data.frame(my_data)
str(my_data)
my_data$var1 = NULL
f20 = as.data.frame(table(my_data$Year20))
f20$Var1 = as.Date(f20$Var1, "%Y-%m-%d")
f20$Var1 = format(f20$Var1, format="%m-%d")
f20$Cumulative_F20 = cumsum(f20$Freq) # cumulative calculation
f20
newcol_20 = c( my_data$Year19,
my_data$Year18, my_data$Year17,
my_data$Year16, my_data$Year15,
my_data$Year14, my_data$Year13,
my_data$Year12, my_data$Year11,
my_data$Year10, my_data$Year9,
my_data$Year8, my_data$Year7,
my_data$Year6, my_data$Year5,
my_data$Year4, my_data$Year3,
my_data$Year2, my_data$Year1)
str(newcol_20)
newdata_20 = data.frame(newcol_20)
str(newdata_20)
newdata_20$newcol_20 = as.Date(as.character(newdata_20$newcol_20), "%Y-%m-%d")
newdata_20$newcol_20 = format(newdata_20$newcol_20, format="%m-%d")
str(newdata_20)
newtable_20 = table(newdata_20$newcol_20)
newtable_20
newdf_20 = as.data.frame(newtable_20)
#newdf_20$Cumulative_20 = cumsum(newdf_20$Freq)/19 # cumulative calculation
newdf_20$Freq = newdf_20$Freq/19
newdf_20
newcol_05 = c( my_data$Year19,
my_data$Year18, my_data$Year17,
my_data$Year16)
str(newcol_05)
newdata_05 = data.frame(newcol_05)
str(newdata_05)
newdata_05$newcol_05 = as.Date(as.character(newdata_05$newcol_05), "%Y-%m-%d")
newdata_05$newcol_05 = format(newdata_05$newcol_05, format="%m-%d")
str(newdata_05)
newtable_05 = table(newdata_05$newcol_05)
newtable_05
newdf_05 = as.data.frame(newtable_05)
newdf_05$Cumulative_05 = cumsum(newdf_05$Freq)/4 # cumulative calculation
newdf_05$Freq = newdf_05$Freq/4
newdf_05
library(ggplot2)
library(ggpubr)
ggplot() +
geom_line(data = newdf_20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1.6) +
geom_line(data = newdf_05, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#999999"), size = 1.6) +
geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#CC79A7"), size = 1.6) +
geom_vline(xintercept = "03-25", color="gray", size=1)+
geom_vline(xintercept = "04-21", color="gray", size=1)+
labs(y = "Cumulative_Frequency", colour= "#000000", size = 16 )+
font("ylab", size = 15, color = "black", face = "bold.italic")+
font("legend.text",size = 10, face = "bold")+
font("legend.title",size = 15, face = "bold")+
theme(axis.line.x = element_line(size = 0.5, colour = "black"), # theme modification
axis.line.y = element_line(size = 0.5, colour = "black"),
#axis.text.x=element_blank(),axis.ticks.x=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.text.x = element_text(colour = "#000000", size = 7,
angle = 90, face ="bold" ),
axis.text.y = element_text(colour = "#000000", size = 12,
angle = 90, face ="bold" ))
Please modify the code and I also added the final output what I am getting need a little bit of modification in the code to get x-axis neatly
One option would be dodging the labels in x-axis:
library(ggplot2)
library(ggpubr)
ggplot() +
geom_line(data = newdf_20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#111111"), size = 1.6) +
geom_line(data = newdf_05, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#999999"), size = 1.6) +
geom_line(data = f20, aes(x=Var1, y=cumsum(Freq), group = 1, color = "#CC79A7"), size = 1.6) +
geom_vline(xintercept = "03-25", color="gray", size=1)+
geom_vline(xintercept = "04-21", color="gray", size=1)+
scale_x_discrete(guide = guide_axis(n.dodge=2))+
labs(y = "Cumulative_Frequency", colour= "#000000", size = 16 )+
font("ylab", size = 15, color = "black", face = "bold.italic")+
font("legend.text",size = 10, face = "bold")+
font("legend.title",size = 15, face = "bold")+
theme(axis.line.x = element_line(size = 0.5, colour = "black"), # theme modification
axis.line.y = element_line(size = 0.5, colour = "black"),
#axis.text.x=element_blank(),axis.ticks.x=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.text.x = element_text(colour = "#000000", size = 7,
angle = 90, face ="bold" ),
axis.text.y = element_text(colour = "#000000", size = 12,
angle = 90, face ="bold" ))
Output:
I searched for many questions already answered, but none of them can help me.
This is my code:
ggplot(z, aes(x=`Fecha de muestreo`, y = Valor, color = `Nombre de la estaciĆ³n`)) +
geom_line(size = 0.4) +
geom_point(size=0.5) +
scale_x_date(date_labels = "%b", breaks = "1 month", limits = c(as.Date("2019-1-1"), as.Date("2019-12-20"))) +
scale_y_continuous("pH (unidades de pH)", limits = c(3, 11), breaks = c(3:11)) +
geom_hline(aes(yintercept = 6.5, linetype = "ECA cat.3 inf D1 y D2 : 6.5"), colour = "Blue", size = 0.4)+
geom_hline(aes(yintercept = 8.4, linetype = "ECA cat.3 sup. D1 : 8.4"), colour = "Green", size = 0.4)+
geom_hline(aes(yintercept = 8.5, linetype = "ECA cat.3 sup. D2 : 8.5"), colour = "red", size = 0.4)+
scale_linetype_manual(name = NULL, values = c(1, 1, 1), # values = tipo de lineas
guide = guide_legend(override.aes = list(color = c("blue", "Green", "Red")))) +
theme(axis.text=element_text(size=6),
legend.margin = unit(-0.2, "cm"),
axis.title=element_text(size=7,face="bold")) +
theme(legend.text=element_text(size=6),
legend.title = element_blank(),
legend.spacing.x = unit(.2, 'cm')) +
theme(legend.key = element_rect(fill = "white")) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid = element_line(colour= "gray"))
This is the graph:
I need to change the order of the legends: the "ECAS" should be at the bottom.
How can I do it?
This should be possible using the order term of guides(). For example:
library(ggplot2)
a <- ggplot(mtcars, aes(wt, mpg,
color = as.character(cyl),
fill = as.character(gear))) +
geom_point(size = 5, shape = 21, stroke = 2) +
scale_color_discrete(name = "cyl as color") +
scale_fill_discrete(name = "gear as fill")
a
To reverse the order, we can add: (note, ordering seems to be from the bottom up)
a +
guides(color = guide_legend(order = 0),
fill = guide_legend(order = 1))
I'm trying to plot a 2D density plot with ggplot, with added marginal histograms. Problem is that the polygon rendering is stupid and needs to be given extra padding to render values outside your axis limits (e.g. in this case I set limits between 0 and 1, because values outside this range have no physical meaning). I still want the density estimate though, because often it's much cleaner than a blocky 2D heatmap.
Is there a way around this problem, besides scrapping ggMarginal entirely and spending another 50 lines of code trying to align histograms?
Unsightly lines:
Now rendering works, but ggMarginal ignores choord_cartesian(), which demolishes the plot:
Data here:
http://pasted.co/b581605a
dataset <- read.csv("~/Desktop/dataset.csv")
library(ggplot2)
library(ggthemes)
library(ggExtra)
plot_center <- ggplot(data = dataset, aes(x = E,
y = S)) +
stat_density2d(aes(fill=..level..),
bins= 8,
geom="polygon",
col = "black",
alpha = 0.5) +
scale_fill_continuous(low = "yellow",
high = "red") +
scale_x_continuous(limits = c(-1,2)) + # Render padding for polygon
scale_y_continuous(limits = c(-1,2)) + #
coord_cartesian(ylim = c(0, 1),
xlim = c(0, 1)) +
theme_tufte(base_size = 15, base_family = "Roboto") +
theme(axis.text = element_text(color = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.text = element_text(size = 12, family = "Roboto"),
legend.title = element_blank(),
legend.position = "none")
ggMarginal(plot_center,
type = "histogram",
col = "black",
fill = "orange",
margins = "both")
You can solve this problem by using xlim() and ylim() instead of coord_cartesian.
dataset <- read.csv("~/Desktop/dataset.csv")
library(ggplot2)
library(ggthemes)
library(ggExtra)
plot_center <- ggplot(data = dataset, aes(x = E,
y = S)) +
stat_density2d(aes(fill=..level..),
bins= 8,
geom="polygon",
col = "black",
alpha = 0.5) +
scale_fill_continuous(low = "yellow",
high = "red") +
scale_x_continuous(limits = c(-1,2)) + # Render padding for polygon
scale_y_continuous(limits = c(-1,2)) + #
xlim(c(0,1)) +
ylim(c(0,1)) +
theme_tufte(base_size = 15, base_family = "Roboto") +
theme(axis.text = element_text(color = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=1),
legend.text = element_text(size = 12, family = "Roboto"),
legend.title = element_blank(),
legend.position = "none")
ggMarginal(plot_center,
type = "histogram",
col = "black",
fill = "orange",
margins = "both")
I need to create a plot, in which a histogram gets overlayed by a density. Here is my result so far using some example data:
library("ggplot2")
set.seed(1234)
a <- round(rnorm(10000, 5, 5), 0)
b <- rnorm(10000, 5, 7)
df <- data.frame(a, b)
ggplot(df) +
geom_histogram(aes(x = a, y = ..density.., col = "histogram", linetype = "histogram"), fill = "blue") +
stat_density(aes(x = b, y = ..density.., col = "density", linetype = "density"), geom = "line") +
scale_color_manual(values = c("red", "white"),
breaks = c("density", "histogram")) +
scale_linetype_manual(values = c("solid", "solid")) +
theme(legend.title = element_blank(),
legend.position = c(.75, .75),
legend.text = element_text(size = 15))
Unfortunately I can not figure out how I can change the symbols in the legend properly. The first symbol should be a relatively thick red line and the second symbol should be a blue box without the white line in the middle.
Based on some internet research, I tried to change different things in scale_linetype_manual and further I tried to use override.aes, but I could not figure out how I would have to use it in this specific case.
EDIT - Here is the best solution based on the very helpful answers below.
ggplot(df) +
geom_histogram(aes(x = a, y = ..density.., linetype = "histogram"),
fill = "blue",
# I added the following 2 lines to keep the white colour arround the histogram.
col = "white") +
scale_linetype_manual(values = c("solid", "solid")) +
stat_density(aes(x = b, y = ..density.., linetype = "density"),
geom = "line", color = "red") +
theme(legend.title = element_blank(),
legend.position = c(.75, .75),
legend.text = element_text(size = 15),
legend.key = element_blank()) +
guides(linetype = guide_legend(override.aes = list(linetype = c(1, 0),
fill = c("white", "blue"),
size = c(1.5, 1.5))))
As you thought, most of the work can be done via override.aes for linetype.
Note I removed color from the aes of both layers to avoid some trouble I was having with the legend box outline. Doing this also avoids the need for the scale_*_* function calls. To set the color of the density line I used color outside of aes.
In override.aes I set the linetype to be solid or blank, the fill to be either white or blue, and the size to be 2 or 0 for the density box and histogram box, respectively.
ggplot(df) +
geom_histogram(aes(x = a, y = ..density.., linetype = "histogram"), fill = "blue") +
stat_density(aes(x = b, y = ..density.., linetype = "density"), geom = "line", color = "red") +
theme(legend.title = element_blank(),
legend.position = c(.75, .75),
legend.text = element_text(size = 15),
legend.key = element_blank()) +
guides(linetype = guide_legend(override.aes = list(linetype = c(1, 0),
fill = c("white", "blue"),
size = c(2, 0))))
The fill and colour aesthetics are labelled by histogram and density respectively, and their values set using scale_*_manual. Doing so maps directly to the desired legend without needing any overrides.
ggplot(df) +
geom_histogram(aes(x = a, y = ..density.., fill = "histogram")) +
stat_density(aes(x = b, y = ..density.., colour="density"), geom = "line") +
scale_fill_manual(values = c("blue")) +
scale_colour_manual(values = c("red")) +
labs(fill="", colour="") +
theme(legend.title = element_blank(),
legend.position = c(.75, .75),
legend.box.just = "left",
legend.background = element_rect(fill=NULL),
legend.key = element_rect(fill=NULL),
legend.text = element_text(size = 15))