How do I increase the distance between the axis.line.y below and the blue geom_hline below? I want a gap between the two, I don't want them kissing as shown on the plot.
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
geom_hline(yintercept = 25, color = "blue") +
theme_minimal() +
theme(axis.line.y = element_line(color = "black"),
axis.ticks.y.left = element_line(color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
I'm not sure if this is exactly what you want but try it.
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
geom_segment(aes(x = min(cty), y = 25,
xend = max(cty), yend = 25),
data = mpg) +
theme_minimal() +
theme(axis.line.y = element_line(color = "black"),
axis.ticks.y.left = element_line(color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
Related
This is my script for the plot,
data = data.frame(Kingdom = c("Bacteria", "Archaea"),
Total = c(273523, 2616))
sizeRange <- c(0,30)
library(ggplot2)
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
somebody, please tell me how can I get a connecting line between my y-axis label and the plot
My plot looks like this
I want something like this
A clean alternative would be to label the points directly, and remove the y-axis if wanted. e.g.:
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
ggrepel::geom_text_repel(aes(label = Kingdom), vjust = -1,colour="black") +
geom_point(aes(size = Total),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"),
axis.text.y=element_blank(),
axis.title.y = element_blank(),
axis.ticks.y=element_blank())
you can manually add segments, but then the alpha of your points will kind of show them.
Here is a try, altought it's not perfect if the x axis expend.
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
# Added the segments here before the points.
# I tried to alpha it but I can't figure out how to limit the
# segment to the point border.
geom_segment(x = rep(-100,2), xend = rep(0,2),
y = c(1, 2), yend = c(1,2),colour="blue", alpha = 10) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() + guides(alpha = "none") + # remove alpha from legend.
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
I have the following graphic:
But I need it to be like this:
My code is:
n_papers<-c(6,4,5,1,6,2,1)
paper_ui<-c(3,2,4,0,1,1,0)
methods<-c("AR","ARMA",'ARIMA',"SARIMA","Loess \n decomposition","Classical \n decomposition","Exponential \n smoothing")
df <- data.frame(n_papers,paper_ui,methods)
ggplot(df) +
aes(x = methods, y = n_papers,fill=paper_ui) +
geom_bar(show.legend = FALSE,stat="identity",position = "dodge")+
labs(x=NULL, y = "Number of papers") +
theme_bw()+
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
And I need the blue to be the same to all the bars color="dodgerblue4". Thank you for the help!
You could add a second geom_bar or geom_col like so:
library(ggplot2)
ggplot(df) +
aes(x = methods) +
geom_col(aes(y = n_papers, fill = "total"))+
geom_col(aes(y = paper_ui, fill = "ui"))+
labs(x=NULL, y = "Number of papers") +
scale_fill_manual(values = c(total = "grey", ui = "dodgerblue4")) +
theme_bw()+
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
I have two plots I would like to combine. My data looks like this:
Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)
This is the first barplot:
tot<-aggregate(mydata$Value,list(mydata$Year),FUN=sum)
tot_y<-tot$x
tot_x<-tot$Group.1
tot_barplot <- ggplot(tot, aes(x=tot_x,y=tot_y)) +
geom_bar(stat = "identity",fill="#73D055FF") +
scale_y_continuous(limits = c(0, 125), breaks = seq(0, 125, by = 25)) +
#xlab("Pathways") +
#ylab("N° of species") +
theme(axis.line = element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_text(size=14,margin=margin(l=10),colour="black"),
axis.ticks = element_blank(),
axis.title=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
tot_barplot
And this is the second plot:
p <- ggplot(mydata, aes(x=Year, y=Name, size = Value)) +
geom_point(aes(fill = Value,
alpha = I(as.numeric(Value > 0))), shape=21, colour = "black") +
scale_fill_viridis_c(option = "D", direction = -1,limits = c(1, 25), breaks=seq(1, 25, 5))+
scale_size_area(guide = "none") +
ylab("Name") +
theme(axis.line = element_blank(),
axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
face="italic"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,face="bold"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_text(size=18))
p
I combine them like this:
grid.arrange(arrangeGrob(tot_barplot,p,nrow=2))
Now I would like to re-size the barplot to fit it better to the second plot (imagine that the original data produce a wider barplot where the bars start above the Name and end above the legend Value). I would like the bars of the barplot to be exactly centred above the line of points and the Year, but I am not very familiar with ggplot aesthetics.
Any suggestion would be appreciated. Thanks!
Hi this is the code I have pls:
library(ggplot2)
ggplot(mpg, aes(cty, hwy))+
geom_point()+
facet_grid( ~ drv) +
theme(panel.margin = unit(.05, "lines"),
panel.border = element_rect(color = "black", fill = NA, size = 1),
strip.background = element_rect(color = "black", size = 1))
I would like to draw a margin between the 3 panels (dotted lines ) and also how can I please remove the 'cty' legend which is appearing on x-axis? Many thanks
Perhaps you are looking for something like this?
library(ggplot2)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid( ~ drv) +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_rect(color = "black"),
axis.title.x = element_blank(),
axis.line = element_line()) +
annotation_custom(grid::linesGrob(gp = grid::gpar(lty = 2)),
xmin = 7.7, xmax = 7.7)
I would like to keep both x axes (bottom and top), while removing the panel border (or both y axes, left and right).
Code:
library(ggplot2)
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") +
theme_bw() +
theme(axis.title.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(size = 0.5, linetype = "solid", colour = "lightgrey"),
axis.line.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank(),
panel.background = element_blank())
print(p1)
This can be accomplished with sec_axis.
In order to reproduce your white background, I'll add theme_bw() before the call to theme; this also helps me break out the lower x-axis line, then I'll add the second axis.
library(ggplot2)
p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") +
theme_bw() +
theme(axis.title.y=element_blank(),
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
panel.border = element_blank(),
axis.line.x = element_line(size = 2, linetype = "solid", colour = "lightgrey"),
axis.line.y = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank())
# original
p1
# both lines
p1 + scale_x_continuous(sec.axis=sec_axis(~.))
(side-by-side here for space/presentation, the code did not do that)
Side note: #chemdork123's suggested answer does work here: use annotate to add a specific geometry. While I don't prefer this method, it can suffice. ('green' retained from the linked answer.)
p1 + annotate(geom = 'segment', y = Inf, yend = Inf, color = 'green', x = -Inf, xend = Inf, size = 4)