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))
Related
I'm plotting a graph and I need to completely remove the legend.key from my ggplot legend. Why I need to do this? The legend starts with a number that reference the X axis breaks, and the label its too large and I don't want to keep it in the X axis. So, in the X axis i put breaks=1:15, and in legend the label starts with this numbers.
In resume, I just want to remove the legend.key from my graph. Is it possible? I have tried legend.key=element_blank(), but without sucess.
Obs.: In the code is it possible to see that I don't want the fill=legto change the colors of each bar. Everything is set to be gray and I just want to remove de legend.key.
ggplot(IC_QS, aes(x=ind,y=values))+
geom_boxplot(aes(fill=leg),color="black", outlier.colour = "red")+
labs(title = "XXXXXXXXXX",
subtitle = "XXXXXXXXXXX",
caption = "XXXXXXXXXXXXX")+
scale_x_discrete(name = "", labels=1:15)+
scale_y_continuous(name = "XXX", breaks = seq(0,10,1), expand = c(0,0.08*max(IC_QS$values)))+
scale_fill_manual(name="Sectors", values = rep("gray", 15), labels=str_wrap(IC_QS_leg,25))+
theme(legend.position = "right", legend.background = element_blank(),
legend.key = element_blank(),legend.text = element_text(face = "bold", size = 8,),
panel.background = element_blank(), panel.grid.major = element_line(colour = "gray", linetype = "dashed"),
axis.title.x = element_text(face = "bold",vjust = -1), axis.title.y = element_text(face="bold", vjust = +1.5),
axis.text = element_text(colour="black", face = "bold"), title = element_text(face = "bold"))
Obviously we don't have your data, but here's an idea using the iris built-in data set
ggplot(iris, aes(Species, Petal.Width, fill = Species)) +
geom_boxplot() +
scale_x_discrete(labels = seq(length(levels(iris$Species)))) +
scale_fill_manual(values = rep("grey", length(levels(iris$Species))),
labels = paste(seq(length(levels(iris$Species))),
levels(iris$Species), sep = " - ")) +
guides(fill = guide_legend(override.aes = list(color = NA, fill = NA))) +
theme_light(base_size = 16) +
theme(legend.key.width = unit(0, "mm"))
I am working with ggplot2 to create plots and I want to increase the size of the labels from the output, so not the labels added manually.
I tried to use cex put this works only partially as it only increases the count size in the plot but not the x or y labels
ggplot(dat, aes(x = Gender, fill = gen))+
geom_bar(position = "dodge")+
scale_fill_manual(values = c("#40E0D0","#DE3163"))+
labs(y="All", fill ="Gender:")+
scale_y_continuous(breaks = seq(0,550,50))+
ggtitle("Gender")+
theme(axis.title.x = element_text(size =20, face = "bold"))+
theme(axis.title.y = element_text(size = 20, face = "bold", angle = 90))+
theme(plot.title = element_text(size = 30, face="bold"), legend.position = "bottom")+
geom_text(aes(label=stat(count), cex = 6), stat = "count",
position=position_dodge(width = 1),vjust= -0.2, cex = 6)+
theme(legend.title = element_text(size = 20))+
theme(legend.text = element_text(size = 20))
``
This is my first time posting a question here so I'm nervous, but this has been bugging me forever: when using facet_wrap() in ggplot, is there a way to keep your plots from getting all squished when you have a lot of them? (see example below)
There is too much range in the data so I have to free the scales, otherwise I would set the y-axis breaks and probably have fewer labels to that from crowding. But the biggest thing is that the plots are so squished they become hard to see. When I put space in between the plots it just squishes them more as though it is working with a limited amount of space. I also tried adjusting font sizes, getting rid of the title, and even the legend. Removing the legend makes more room but obviously, that's sort of an important thing to have in there!
Is there a way to fix this or is this just something that goes with trying to facet a lot of plots?
Here is my code:
lu_acres_plot <- ggplot(data = land_use_acres, aes(x = land_use, y = acres)) +
geom_col(aes(fill = land_use)) +
facet_wrap(~i_desclu, scales = "free_y") +
labs(title = "Land Use Change",
y = "Change in Acreage") +
theme_minimal() +
geom_hline(aes(yintercept=0)) +
scale_fill_manual(values = lu_pallette) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
text = element_text(family = "Times"),
axis.text = element_text(size = 12),
axis.title = element_text(size = 14, face = "bold"),
plot.title = element_text(size = 16, face = "bold"),
legend.position = "bottom",
legend.title = element_text(size = 14, face = "bold"),
legend.title.align = 0.5,
strip.background = element_rect(color = "grey40", fill = "grey30"),
strip.text = element_text(size = 9, color = "white", face = "bold"),
panel.border = element_rect(color = "grey40", fill = NA)
) +
guides(fill = guide_legend(title = "Land Use",
title.position = "top",
nrow = 2))
lu_acres_plot
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)
I am having some trouble displaying the size legend in my plot and changing the name of my size legend.
My data is corp already has a size column which is either of the values 5, 10, 20
I am using ggplot2 I already have a legend for the color
I want to add one for the size and manually change the size labels..
How do I increase the the font of the legend ? It's super tiny (FIN, IND UTIL)
also the 15 for the size shouldnt be there i want to just omit it and display both legends side by side.
p <- ggplot(corp, aes(x=annRisk, y=annRet, color = corp$subsector1, face = "bold"))
p<- p + geom_point(aes(size = corp$Colsize), alpha = 0.55)
p<-p + scale_size(range = c(8, 20))
p<-p + scale_colour_manual("", values = c("UTIL" = "#fdcc8b", "IND" = "#fc8d59", "FIN" = "#d7301f",
"ABS" = "#74a9cf", "CMBS" = "#0570b0", "LA" = "#8c96c6", "SOV"= "#88419d", "SUPRA" = "#b3cde3"))
p<-p+labs(title = "SOME TITLE")
print(p)
p<-p+theme(plot.title = element_text(face = "bold", size = 20))
p<-p+theme(axis.title.x = element_text(size = 20), axis.text.x = element_text(size = 13))
p<-p+theme(axis.title.y = element_text(size = 20), axis.text.y = element_text(size = 13))
p<-p+geom_text(aes(label=ifelse(Colsize>=10,subsector2,"")), size=5,color = "black", face = "bold", hjust=-0.1, vjust = 0.1)
p<-p+scale_x_continuous(labels = percent, name = "Annualized Risk", limits = c(0.05, 0.09))
p<-p+scale_y_continuous(labels = percent, name = "Annualized Return", limits = c(0.04, 0.08))
p<-p+ theme(legend.position = "bottom")
print(p)
Although I can't use your data yet, you can try adding the following code:
p <- p + theme(legend.position = "bottom",
legend.title = element_blank(),
legend.text = element_text(size=14),
legend.box = "horizontal")
p <- p + scale_size_manual(values=c(5,10,20), labels = c("5","10","20"))