I have created a bar chart which shows the sales of products in a particular category. This is the bar chart. As you can see it is not very clear so I am trying to set limits for the Y axis.
I create the bar chart with the following line:
bakerySales <- ggplot(sales_bakery, aes(ProductName, ProductSales))+
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend =
FALSE)
I then go on to apply a theme to the bar chart using:
bakerySales <- bakerySales +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(colour = "black", size = 14, angle = 60,
hjust = 1),
axis.text.y = element_text(colour = "black", size = 14),
panel.background = element_rect(fill = "white"),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
axis.line = element_line(colour = "black", size = 1),
legend.position = "none",
plot.title = element_text(lineheight = 8, face = "bold"))
I have tried to set the limits for the y axis using:
bakerySales <- bakerySales + ylim(5000,10000)
When I do this I lose the content of the bar chart, It looks like this.
Can someone please tell me where I am going wrong.
Thanks
If you want to zoom in on specifix ylimits, you could use the coord_cartesian function. I do not have the bakerysales dataset, this is an example using mtcars data:
ggplot(mtcars, aes(x = gear, y = qsec)) +
stat_summary(fun.y=sum,geom="bar",colour="red",fill="red",show.legend = FALSE) +
coord_cartesian(ylim = c(200, 300))
Maybe you want
+ coord_cartesian(ylim = c(5000,10000))
df <- data.frame(x = c("a","b"), y = c(1000, 2000))
ggplot(df, aes(x=x,y=y)) +
geom_bar(stat="identity") +
coord_cartesian(ylim = c(500,3000))
Related
My question is very similar to this question, but it's not the same.
I am looking for a way to create an empty ggplot with just the legend. However, in contrast to the autohor of the question I linked at the top, I actually need to create just the legend with no plot area included in the image.
I tried the following code:
ggplot(NULL, aes(color = ""))+
geom_blank()+
scale_color_manual(values = "black", labels = "Something")+
guides(color = guide_legend())+
theme(legend.box.background = element_rect(color = "black"))
But I'm getting the opposite of what I want - I am getting an empty plot area with no legend, like this:
And I would like my end result to look like this (I drew this in Paint):
Any help would be appreciated!
You can make a normal plot, then play with theme to achieve the desired result.
library(ggplot2)
ggplot(data.frame(x = 1, y = 1, colour = 'Something'), aes(x, y, fill = colour))+
geom_point(alpha=0, shape = 0)+ # completely transparent rectangular point
scale_fill_manual(values='black', drop=FALSE) +
guides(fill = guide_legend(override.aes = list(alpha=1, size = 40)))+ # showing the point in the legend
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = c(0.5, 0.5), # move the legend to the center
legend.title = element_blank(),
legend.text = element_text(size = 40),
legend.key = element_rect(fill='NA'),
panel.grid = element_blank(),
panel.border = element_rect(colour = "black", fill='white', size=1)
)
Get the legend how you want it to look, then extract it with cowplot::get_legend:
library(grid)
library(cowplot)
library(ggplot2)
grid.newpage()
grid.draw(get_legend(
ggplot(data.frame(x = 1, y = 1), aes(x, y, fill = "Something")) +
geom_col(size = 20)+
scale_fill_manual(values = "white", labels = "Something", name = "") +
theme_bw() +
theme(legend.box.background = element_rect(color = "black"),
legend.title = element_text(size = 30),
legend.key.size = unit(60, "points"),
legend.text = element_text(size = 24),
legend.key = element_rect(colour = "black"),
legend.box.margin = margin(20, 20, 20, 20))))
I have a plot for which I would like to place one facet label in the top and the other one on the left side of the plot. However, I don't know if that is possible or if I should do it in an other way. Below I show a fake example to play with:
library(ggplot2)
library(ggsci)
library(ggthemes)
library(ggh4x)
df <- data.frame(x=c(0,0,0.3,0.8,1.5,3,5,7,9,13,15,20,28),
y=c(0,0,0.3,0.8,1.5,3,5,7,9,13,15,20,28))
df$Method <- "Pearson"
df$ID <- "A"
Plot <- ggplot(df, aes(x=x, y=y)) +
geom_point(size=.8) +
theme_hc() +
theme(strip.background = element_rect(colour = "black", fill = "white",
size = 1.5, linetype = "solid"),
axis.title.x =element_blank(),
axis.title.y =element_blank(),
axis.text.x = element_text(angle = 0, hjust = 0.5,size = 10.5, face="bold"),
axis.text.y = element_text(angle = 0, hjust = 0.5,size = 10.5),
strip.text.x = element_text(size = 9),
strip.text.y = element_text(size = 13),
axis.line = element_line(),
panel.grid.major= element_blank(),
panel.grid.minor = element_blank(),
legend.text=element_text(size=9),
legend.title = element_text(size=10,face="bold"),
legend.key=element_blank(),
legend.justification = c(0.5,0),
legend.position = "right",
panel.border = element_blank(),
strip.placement = "outside",
plot.title = element_text(size = 16, hjust = 0.5),
strip.switch.pad.grid = unit('0.1', "cm")) +
labs(x= '\nTime delay (modifiable device)',y=expression(R^{2})) +
guides(color=guide_legend(override.aes=list(fill=NA))) +
scale_y_continuous(limits = c(0., 30), breaks = c(5, 15,25)) +
facet_wrap(Method~ID) +
scale_color_jco()
Plot
I would like to place the A label in the left part of the plot? Does anyone know how to do it?
Thanks
I'm sorry for stripping away most of the plotting code, but the following captures the essence and requires less additional packages.
facet_wrap() only puts strips on one side of the plot. If you need two sides with strips, you can use facet_grid(). To place the strip on the left, you can use the switch = 'y' argument.
library(ggplot2)
df <- data.frame(x=c(0,0,0.3,0.8,1.5,3,5,7,9,13,15,20,28),
y=c(0,0,0.3,0.8,1.5,3,5,7,9,13,15,20,28))
df$Method <- "Pearson"
df$ID <- "A"
ggplot(df, aes(x=x, y=y)) +
geom_point(size=.8) +
facet_grid(Method~ID, switch = 'y')
I am trying to customize the strip in facet_grid function.
My graph currently looks like:
I successfully changed the background of strip to purple, however, I could not change the border color to black, even though I set the color to 'black' in the function.
Also, I want the space between text and the rectangle to be larger so that it will looks nicer. How should I achieve this?
My codes looks like:
plot.density <- ggplot(df_densityWindow, aes(x = idx, y = density, color = factor(location))) +
geom_bar(stat = 'identity', fill = 'white') +
facet_grid(marker ~ case, scales = 'free') +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF")) +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
background_grid(major = 'y', minor = "none") + # add thin horizontal lines
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(axis.title = element_text(size = 28)) +
theme(axis.text = element_text(size = 26)) +
theme(legend.text = element_text(size = 16)) +
theme(legend.title = element_text(size = 18)) +
panel_border() # and a border around each panel
plot(plot.density)
If necessary, the data could be downloaded here:
data
Thank you!
Your colour specification for strips works without error for me. The spacing between the strip text and box can be increased by setting the margin argument at the strip.text theme:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
facet_grid(Species ~ rev(Species)) +
theme(strip.background = element_rect(colour="red", fill="#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)))
Possible debug strategy:
Reproduce code above
1A. If that doesn't work, check if your ggplot versions is ~3.2-ish
1B. If that does work, proceed to 2.
Try to remove and place back lines of plotting to see where the error occurs
I'm particularly unfamiliar with panel_border() and background_grid(), so you could try those first.
EDIT: Plot based on data and code provided
ggplot(data, aes(x = idx, y = density, colour = factor(location))) +
geom_col(fill = "white") +
scale_color_manual(name = 'Regions',values = c("#F26969", "#02f527",'#F2F2C6')) +
facet_grid(marker ~ case, scales = "free") +
xlab('Index') +
ylab(expression(paste('Density/', mm^2, ))) +
theme(strip.background = element_rect(colour = "red", fill = "#CCCCFF"),
strip.text = element_text(margin = margin(10, 10, 10, 10)),
axis.title = element_text(size = 28),
axis.text = element_text(size = 26),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
# Improvised based on missing functions
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(colour = "grey90"),
panel.background = element_rect(fill = "white", colour = NA),
panel.border = element_rect(colour = "grey90", fill = NA))
I have the following data:
fixed <- as.data.frame(rep(0.125,8))
fixed[,2] <- c("1","2","3","4","5","6","7","8")
colnames(fixed) <- c("Percentage", "Test")
and produced the following graph:
Now I would like to achive something like this. However, I have not found a way to draw outside of the graph area and write separate axis labels. Is that possible?
The code for my plot is here:
p <- ggplot(fixed,aes(x=Test ,y=Percentage,fill=Test))+
geom_boxplot()+
stat_summary(fun.data = function(y)
data.frame(y=0.6, label = paste(round(mean(y),2))), geom="text",size=3) +
geom_hline(yintercept=0.55, linetype="dashed", color = "black")+
theme(legend.position="none")+
scale_y_continuous(limits = c(0, 0.6),breaks=seq(0,0.6,0.1),
labels= c("0%","10%","20%","30%","40%","50%","Mean"))+
theme_bw()+
labs(title = "Title")+
xlab("Test")+
ylab("Percetage")+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey90", colour = NA),
plot.background = element_rect(fill = "grey90", colour = NA),
legend.position="none",plot.title = element_text(size= 12, hjust=0.5),
plot.margin = unit(c(1,1,1,1), "cm"))+
scale_fill_manual(values=c("#00441b","#006d2c","#238b45","#41ab5d","#74c476",
"#a1d99b","#c7e9c0","#e5f5e0"))
p
I'm plotting different types of graphs where my legend has the colorbar of the values in my graph in log like in the image below:
The code that gives something like this is:
2Dmap <- ggplot(x, aes(L, W, fill = R)) +
geom_raster() + coord_fixed() +
scale_fill_viridis(name="R", trans = "log", direction = -1, labels=trans_format('log10',math_format(10^.x)), breaks=cycle.breaks,
limits=c(10^(floor(log10(min(x$R[x$R > 0] )))), 10^(ceiling(log10(max(x$R)))) ) )+
theme_bw() + coord_fixed(ratio=12)
2Dwmap <- 2Dwmap + theme(legend.position="bottom") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
theme(legend.key.width=unit(2.2, "cm")) +
theme(legend.text=element_text(size=15)) +
theme(legend.position="bottom",
title = element_text(size = 20),
axis.title.x=element_text(size = 20),
axis.title.y=element_text(size = 20),
axis.text.x = element_text(size = 18),
axis.text.y = element_text(size = 18),
legend.title = element_text(size = 18),
legend.text = element_text(size = 16))
So, I'm trying to look for a good way to avoid my limiting breaks to be cut off.
Any1 any ideas ?
Sorry I cannot share data to make a reproducible example :)