I am currently using a facet_wrap to knit data (html) to an Rmarkdown document. I am looking to use facet_wrap and ggplot to show this data.
This is how the data looks just in ggplot (not plotly):
However, when I use plotly the titles of each plot intersect with the y-axis making interpretability difficult
Here is the code to the first plot not using plotly
plot <- ggplot(dataframe, aes(x = week, y = measure))+
geom_line(size = 1.25, aes(color = "orange"))+
geom_point(size = 1.50)+
theme_economist()+
geom_smooth(method = 'auto', se = FALSE, size = .50)+
scale_x_continuous(breaks=seq(0,13,1))+
scale_y_continuous(labels = scales::dollar_format(scale = .0001, suffix = "K"))+
facet_wrap(vars(category), scales = "free", ncol = 3, nrow = 6)+
theme(legend.position = "none")+
ggtitle('Title')+
theme(
panel.grid.major = element_line(linetype = "dotted"),
axis.title.x = element_text( size = 10, margin=margin(30,0,0,0)),
axis.title.y = element_text( size = 10, margin=margin(0,30,0,0)),
axis.text = element_text( size = 10),
plot.title = element_text( size = 14, margin=margin(0,0,35,0), hjust = 0.5),
panel.background = element_rect(fill = NA),
strip.text = element_text(size=10)
)
Then here is what I do to conver it to plotly
ggplotly(plot)
Some random data:
require(stats)
r <- function(x) {abs(as.numeric(arima.sim(n=x, list(order=c(1,0,0),ar=0.95)))+10)}
dataframe = data.frame(week = rep(1:13,6), measure = r(13*6), category = rep(as.character(1:6),each=13))
Although I was not able to reproduce the overlapping y-axis labels, you can set the margins between the faceted plots by setting panel.margin.y and panel.margin.x inside the theme():
plot <- ggplot(dataframe, aes(x = week, y = measure))+
geom_line(size = 1.25, aes(color = "orange"))+
geom_point(size = 1.50)+
theme_economist() +
geom_smooth(method = 'auto', se = FALSE, size = .50)+
scale_x_continuous(breaks=seq(0,13,1))+
scale_y_continuous(labels = scales::dollar_format(scale = .0001, suffix = "K"))+
facet_wrap(vars(category), scales = "free", ncol = 3, nrow = 6)+
theme(legend.position = "none")+
ggtitle('Title')+
theme(
panel.grid.major = element_line(linetype = "dotted"),
axis.title.x = element_text( size = 10, margin=margin(30,0,0,0)),
axis.title.y = element_text( size = 10, margin=margin(0,30,0,0)),
axis.text = element_text( size = 10),
plot.title = element_text( size = 14, margin=margin(0,0,35,0), hjust = 0.5),
panel.background = element_rect(fill = NA),
strip.text = element_text(size=10), panel.margin.x = unit(1,"lines"), panel.margin.y = unit(0,"lines")
)
ggplotly(plot)
You can fine tune the margins to suit your plot.
Before:
> ggplotly(plot)
After:
> ggplotly(plot)
Related
For one of the presentations, I am trying to make a scatter plot. Here is a similar code I am trying to run for the visualization I would like to have
library(tidyverse)
library(showtext)
library(ggtext)
ggplot(mtcars, aes(x = mpg, y = disp)) +
geom_point(size = 1.5) +
labs(title = "<span style = 'color:#005A9C;'>A vs. B</span>",
x = "B<br>BBBBB<br>BBBBBBBBBB<br>BBBBBBBBBB",
y = "A<br>AAAAA<br>AAAAAAAAAA<br>AAAAAAAAAA",
caption = expression(paste(italic("Source:"), " JJJJJ"))) +
theme_minimal() +
theme(
plot.background = element_rect(fill = "white",colour = NA),
panel.background = element_rect(fill = 'white', colour = 'white'),
panel.border = element_blank(),
legend.position = "none",
plot.title = element_markdown(hjust = 0.5, family = "title", size = 45),
plot.subtitle = element_markdown(hjust = 0.5, family = "subtitle", size = 30), # note that to color numbers differently, I used element_markdown not element_text function
axis.title.x = element_markdown(hjust = 0.5, family = "subtitle", size = 33),
axis.text.x = element_markdown(hjust = 0.5, family = "subtitle", size = 20),
axis.title.y = element_markdown(hjust = 0.5, family = "subtitle", size = 33),
axis.text.y = element_markdown(hjust = 0.5, family = "subtitle", size = 20),
plot.caption = element_text(hjust = 0, size = 21, family = "caption"))
ggsave("plot.png", height = 6, width = 7)
And here is the result;
Now, my question is how to reduce the distance between text lines for the x and y axes?
I have the following bubble plot that shows the abundance percentage of microbes across different samples. However, I want to remove the tick labels called "Archaea" and "Other taxa" (located at either ends of the bubble plot) since the labels for both can be placed in the x-axis strip text instead. I used the following code to produce the plot:
ggplot(En.TaxMisc.NoC.RelAb.filtered.tidy$CombinedMisc,
aes(x = factor(Taxonomy, levels = En.TaxMisc.order$Taxonomy),
y = SampleSource, size = RelAb)) +
geom_point(colour = '#abd9e9') +
facet_grid(SampleType ~ Level,
labeller = labeller(SampleType = SampleType.NoC.labels),
scale = 'free', space = 'free') +
scale_x_discrete(name = NULL) +
scale_y_discrete(position = 'left', name = NULL) +
scale_size_continuous(name = str_wrap('Relative abundances (%)', width = 10),
breaks = c(1:8), range = c(0.75, 20)) +
guides(size = guide_legend(nrow = 1)) +
theme(legend.position = 'bottom',
legend.background = element_rect(colour = 'grey70'),
legend.title = element_text(size = 8, hjust = 1),
legend.text = element_text(size = 7, hjust = 0),
legend.spacing.x = unit(2.5, 'mm'),
legend.box = 'horizontal',
strip.background = element_rect(colour = 'grey55'),
strip.text.x = element_text(size = 8),
strip.text.y = element_text(size = 8),
axis.text.x.bottom = element_text(angle = 90, hjust = 1,
vjust = 0.3, size = 8),
axis.text.y.left = element_text(size = 8),
axis.ticks = element_blank(),
panel.grid.major.x = element_line(linetype = 1),
panel.border = element_rect(linetype = 1, fill = NA),
panel.background = element_blank())
I had tried to use scale_x_discrete(labels = c("Archaea" = NULL, "Other taxa" = NULL) but this resulted in all the tick labels being removed. I had also looked into using the rremove() function and the axis_ticks theme components, but neither appear to possess arguments for specifying tick labels.
I'd appreciate suggestions or advice anyone can give me!
There's a fair bit of extraneous detail in the question, but if you're just looking to remove (or customize!) tick labels, all you need is to add a labels argument to scale_x_discrete.
Self-contained example:
library(ggplot2)
ds = data.frame(
xVar = as.factor(rep(LETTERS[1:5],10)),
y = rnorm(50)
)
my_custom_labels = c("","level B","level C","level D!","")
ggplot(data = ds) +
geom_point(aes(x = xVar,y = y)) +
scale_x_discrete(labels = my_custom_labels)
Please, find My script below.
I have produced this plot
However, for graphic purposes, I would like to rearrange the horizontal legend to be presented as 2x2. I have manually produced an example of what it should look like in Photoshop:
Unfortunately, I have not been able to locate any clear instructions.
My script
ggplot(as.data.frame(out), aes(x = n.fjernet)) + theme +
geom_ribbon(aes(fill = model, ymin = lower, ymax = upper), alpha = .1) +
geom_line(aes(y = yhat, col = model),size=1) +
ggtitle("Lymph node yield") +
geom_segment(aes(x = 0, y = 1, xend = 100, yend = 1), lty="dashed", size=0.5) +
geom_segment(aes(x = 25, y = 1, xend = 25, yend = 0.5), lty="dashed", size=0.5, col="black") +
geom_point(mapping = aes(x = 25, y = 1), size=2, shape=16, col="black", alpha=0.5) +
scale_fill_manual(values = c("#DAE5F2", "#F9E7E5","#E4F2F3","#FAF1D9"), name = "",
labels = c("Overall survival (without LNY)", "Event-free survival (without LNY)","Overall survival (with LNY)", "Event-free survival (with LNY)")) +
scale_colour_manual(values = c("#2C77BF", "#E38072","#6DBCC3","#E1B930"), name = "",
labels = c("Overall survival (without LNY)", "Event-free survival (without LNY)","Overall survival (with LNY)", "Event-free survival (with LNY)")) +
scale_x_continuous(name="", breaks=seq(0,100,by=25), limits=c(0,100), label=c("0","25\nas reference","50", "75", "100")) +
scale_y_continuous(name="Hazard ratio", breaks = seq(0.5,1.2,by=.1)) +coord_cartesian(ylim=c(0.5,1.25)) +
theme(axis.text.x = element_text(color = "grey20", size =11),
axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
axis.text.y = element_text(color = "grey20", size = 11),
axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
legend.key = element_rect(fill = "white"),
plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top")
Using the dataset data(iris),part of your code and guides(col = guide_legend(nrow=2, ncol=2)), I have achieved the following:
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length))+
geom_point(aes(y = Sepal.Width, col = Species)) +
theme(axis.text.x = element_text(color = "grey20", size =11),
axis.title.x = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(t=12)),
axis.text.y = element_text(color = "grey20", size = 11),
axis.title.y = element_text(color = "grey20", size = 14, face="bold", margin=ggplot2::margin(r=12)),
legend.key = element_rect(fill = "white"),
plot.title = element_text(color = "grey20", size = 18,face="bold",hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.text=element_text(size=12), legend.title=element_text(size=14), legend.position="top")+
guides(col = guide_legend(title.position = "top",nrow = 2))
Be aware that if you do fill, then in guides() you should also use fill. I have used col since it is what creates my legend.
You can try guides
Let's say you store your plot in p, you could write:
p + guides(col = guide_legend(nrow=2, ncol=2))
The solution was
p + guides(fill=guide_legend(nrow=2,byrow=TRUE))
When storing my ggplot in p
I want to create space between the titles (the axis title and the plot title) and the edge of the plot. I tried vjust on axis.title and plot.title with no luck. Nothing really changed in the plot when I tried various values for vjust. I also tried plot.margin, but nothing seemed to happen with it either.
Data:
data = data.frame(Category = c(0,1), value = c(40000, 120000))
data$Category = factor(data$Category, levels = c(0,1), labels = c("One-time", "Repeat"))
Plot:
p = ggplot(data, aes(Category, Value)) +
geom_bar(stat = "identity", width = 0.5, position=position_dodge(width=0.9)) +
geom_text(aes(label=Value), position=position_dodge(width=0.9), family = "mono", vjust=-0.5) +
ggtitle("Title") +
scale_y_continuous(expand = c(0,0), limits = c(0,150000)) +
scale_x_discrete(expand = c(0,0), limits = c("One-time", "Repeat")) +
xlab("X axis Title") +
ylab("Y axis Title")
Theme:
p + theme(
panel.grid.major = element_line(linetype = "blank"),
panel.grid.minor = element_line(linetype = "blank"),
axis.title = element_text(family = "sans", size = 15),
axis.text = element_text(family = "mono", size = 12),
plot.title = element_text(family = "sans", size = 18),
panel.background = element_rect(fill = NA)
)
You want to do this using margin
p + theme(
panel.grid.major = element_line(linetype = "blank"),
panel.grid.minor = element_line(linetype = "blank"),
axis.title.x = element_text(family = "sans", size = 15, margin=margin(30,0,0,0)),
axis.title.y = element_text(family = "sans", size = 15, margin=margin(0,30,0,0)),
axis.text = element_text(family = "mono", size = 12),
plot.title = element_text(family = "sans", size = 18, margin=margin(0,0,30,0)),
panel.background = element_rect(fill = NA)
)
Note that margin requires a four inputs and they specify the space in the order top,right,bottom,left. Also note I'm using the developmental ggplot2 version so my title default is left justified. 'hjust' and 'vjust' worked in older versions of ggplot2.
You can also give a "-ve" value to margin argument to pull the title closer to the plot.
gg_1 +
theme(plot.title = element_text(size = 12,
hjust = 0.5,
family = fam_3,
face = "bold",
margin = margin(0,0,-10,0))
I created a ggplot2 object:
a <- replicate(8,rnorm(100))
colnames(a) <- letters[1:8]
b < -melt(a,id.vars=1:1)
colnames(b) <- c("c","variable","value")
ggplot(b,aes(x = c,y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = factor(variable)), size = 1.7) +
scale_x_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand=c(0.01, 0.01)) +
scale_y_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand = c(0.01, 0.01))+
theme_bw(base_size = 12, base_family = "Helvetica") +
theme(axis.text=element_text(size = 10),
axis.title=element_text(size = 10),
text = element_text(size = 10),
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_rect(colour = "black", fill = NA, size = 0.5),
panel.background = element_blank(),
legend.position = "top" ,
legend.direction = "vertical",
legend.title = element_blank(),
legend.text = element_text(size = 13),
legend.background = element_blank(),
legend.key = element_blank()) +
labs(x = '', y = '', title = "") +
theme(plot.title = element_text(size=10)) +
theme(strip.text.x = element_text(size = 8,color="black"),
strip.background = element_blank()) +
theme(strip.text.x = element_text(size = 8, colour = "black"))
My problem is the following:
when I create the legend, there is a separate legend for the colors and a separate one for the points.
How can I create a single legend for each of the 8 variables?
Let me minimise your code and focus on the legend issue. This is what you have now.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line() +
geom_point(aes(shape = factor(variable)),size=1.7)
Your data frame, b has variable as factor. You use this in two ways here; variable and factor(variable). You can simply use variable for shape in geom_point; make all variable identical.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = variable),size = 1.7)
I saw some warning messages related to colours and other things. You may want to take care of them. But, for legend, this is one way to go.
Take from the ideas on this page: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels
I edited your code to make the data visible (you had problems with your x-axis limits. Note the final three lines. These commands tell ggplot to create only one legend.
a<-replicate(6,rnorm(100))
colnames(a)<-letters[1:6]
b<-melt(a,id.vars=1:1)
colnames(b)<-c("c","variable","value")
ggplot(b,aes(x=c,y=value,colour=variable,linetype=variable)) +
geom_line() + geom_point(aes(shape=factor(variable)),size=1.7)+
scale_x_continuous(limits=c(0,100))+
scale_y_continuous(limits=c(-2,2),breaks=seq(-2,2,0.1),expand=c(0.01,0.01))+
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
text = element_text(size=10),
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_rect(colour="black",fill=NA,size=0.5),
panel.background = element_blank(),
legend.position="top" ,
legend.direction="vertical",
legend.title=element_blank(),
legend.text=element_text(size=13),
legend.background=element_blank(),
legend.key=element_blank())+
labs(x='', y='',title="")+
theme(plot.title=element_text(size=10))+
theme(strip.text.x = element_text(size = 8,color="black"),strip.background=element_blank())+
theme(strip.text.x = element_text(size = 8,color="black"))+
scale_colour_discrete(name ="Factor")+
scale_linetype_discrete(name ="Factor") +
scale_shape_discrete(name ="Factor")