Make legend symbols smaller than default in R waffle plot - r

I have a two-part waffle plot with a lot of little squares, and a legend that contains two squares. I would like to make the sizes of all of these squares to be the same.
There was an issue opened on GitHub about this, and the repo owner said that since waffle() returns a ggplot2 object, we can use guide() to do this.
I tried searching on documentation to do this and came up with
library(waffle)
phrase_count = 17345/10000
all_count = (22784085 - phrase_count)/10000
my_waffle = waffle(c("All"=all_count, "Phrases"=phrase_count),
rows=43,
size=0.6,
colors=c("#969696", "pink", "white"),
flip=TRUE)
my_waffle + guides(colour=guide_legend(override.aes = list(size=0.6)))
but this doesn't affect the size of the legend. I've seen people use color, colour, or shape, but none of these arguments work for me.
How do I get the size of squares in the legend to be the same as the size of the squares in the plot itself?

Try setting theme() for your legend key as the waffle object is from ggplot2 nature as mentioned in comments by #Waldi:
#Code
my_waffle <- my_waffle + theme(legend.key.size = unit(3, "mm"))
Output:
Or maybe this:
#Code 2
my_waffle + theme(legend.key.height = unit(0.2, "cm"),
legend.key.width = unit(0.3, "cm"))
Output:

Related

Is it possible to make these adjustments to a ggcorrplot() while preserving a complete theme?

I've created a correlation matrix:
cor_matrix = cor(qual_colleges_all_data_clean[ ,c(4,5,8,9,10,12,13,14,16,20,21,22,23,25)], method='pearson',use='complete.obs')
ggcorrplot(cor_matrix, hc.order = TRUE, method ='circle', type='lower', colors = c("darkblue", "white", "red")) +
labs(title = "Correlation Matrix:", subtitle = "Select Variables") +
theme_minimal() +
theme(
legend.key.width = unit(0.6, "cm"),
legend.key.height = unit(1.3, "cm"),
axis.text.x = element_text(angle = 45) #to put x-axis labels at 45 angles
)
Here's what it looks like:
Initially the x-axis labels were all horizontal and thus were a jumbled mess, so I angled them. But as you can see, they're overlapping the plot. The problem is that I need to preserve theme_minimal() if at all possible. I've tried every theme() adjustment I could find online, but I cannot make anything work. So, I have three questions:
How can adjust the x-axis headings while preserving theme_minimal()?
How can I widen the plot while preserving theme_minimal()? My hope is that will space the circles a bit more suitably.
How can I eliminate the Var2 and Var1 axis labels while preserving theme_minimal()?
I realize these are basic questions, so I really appreciate you taking the time to help me. Thanks in advance!

Drawing ggplot Footer Using linesGrob within grobTree

I have created an extensive theme to plot in ggplot similar to Five Thirty Eight. Without using ggthemes, how can I utilize linesGrob to draw a line above my footer that is centered and goes 85% of the way across the plot without touching the edges of the plot? I am striving for the footer similar to this plot.
I can edit the font text, color, and size so don't worry about that.
So far, what I have is this:
data(iris)
library(ggplot2)
library(grid)
library(gridExtra)
plot20 <- ggplot(iris,aes(x=Petal.Length,y=Sepal.Length,color=Species)) +
geom_point(alpha=0.5,size=5) +
ylab("") +
xlab("") +
theme(panel.grid.minor.y=element_blank(),
panel.grid.major.x=element_line(color="#D2D2D2",size=0.7),
panel.grid.major.y=element_line(color="#D2D2D2",size=0.7),
panel.grid.minor.x=element_blank(),
panel.background = element_rect(fill = '#F0F0F0',colour=NA),
plot.background = element_rect(fill = '#F0F0F0', colour=NA, size = 4),
legend.background=element_rect(fill="#F0F0F0"),
legend.key=element_blank(),
legend.title=element_text(face="bold"),
axis.text=element_text(face="bold"),
legend.position="none",
axis.ticks=element_blank())
#Plot Header
my_g2 <- grobTree(rectGrob(gp=gpar(fill='#F0F0F0',col=NA)),
textGrob("Iris Dataset",x=0.115, vjust = -0.5,gp=gpar(fontsize=18,fontface="bold")),
textGrob("This is a subheader for the iris dataset",x=0.235,vjust=1.5,gp=gpar(fontsize=14)))
#Plot Footer
my_g1 <- grobTree(rectGrob(gp=gpar(fill="#F0F0F0",col=NA)),
textGrob(" medavis6",x=0,hjust=0,gp=gpar(col="darkorange",fontsize=8,fontface="bold")),
textGrob("Source: R",x=.85,hjust=-1.06,gp=gpar(col="black",fontsize=8)))
#Plot All Together
allplot <- grid.arrange(my_g2,plot20,my_g1,heights=c(1.17,11,0.5))
Which gives me this.
I think I should be using linesGrob() within my footer grobTree(), but whenever I try to do it I cannot make it appear in my plots. I'm not sure if my rectGrob() is plotting over the top of it or what is happening.
Thanks for any and all help and please, let me know if you need any clarification. Also, if any of my code is poorly written, I'm always looking for constructive criticism to make it better!
I also used linesGrob
#Plot Footer
my_g1 <- grobTree(rectGrob(gp=gpar(fill="#F0F0F0",col=NA)),
linesGrob(unit(c(.05, .95), "npc"), unit(1, "npc"),
gp = gpar(col = 'lightgrey', lwd = 4)),
textGrob(" medavis6",x=0,hjust=0,gp=gpar(col="darkorange",fontsize=8,fontface="bold")),
textGrob("Source: R",x=.85,hjust=-1.06,gp=gpar(col="black",fontsize=8)))
#Plot All Together
allplot <- grid.arrange(my_g2,plot20,my_g1,heights=c(1.17,11,0.5))
grid.draw(allplot)

Graphical parameters in ggplot2: How to change axis/tick thickness

I just started using 'ggplot2', and am running into some problems with the graphical usability.
I wanted to do a simple regression biplot. However, I am not quite convinced by the themes offered by 'ggplot2' and 'ggthemes'.
My code thus far is as follows:
ggplot(data, aes(APE.15N, APE.13C)) +
geom_point(size=3) +
geom_smooth(method="lm", se=F, col="black") +
theme_light(base_size = 20) +
annotate("text", x=.9, y=1.35, label="R²=0.3192, p<0.001", size=6.5) +
coord_cartesian(xlim = c(.25, 1.1), ylim = c(1.05, 2.55)) +
ylab(expression(paste('APE '^{13}, "C", sep = ""))) +
xlab(expression(paste('APE '^{15}, "N", sep = "")))
...which gives me the following plot:
ouput from R with ggplot2
Now, I would like to increase the axis-line thickness as well as the tick thickness to at least 2 points, add minor ticks, get rid of the background grid, and change the axis colour to black.
I just can't figure out how...
I would imagine the result as in the following graph:
example graph
Thank you,
your help is very much appreciated
To increase the axis-line thickness and change the color to black:
axis.line = element_line(colour = 'black', size = 2)
To increase the tick thickness:
axis.ticks = element_line(colour = "black", size = 2)
To add minor ticks:
Minor ticks are not currently an option of ggplot2. There are many other stackoverflow questions about minor ticks that I would suggest looking at. You can try adding minor_breaks in scale_x_continuous, but that would require a knowing the actual minor ticks you want.
To remove the background grid:
panel.grid.major = element_blank(), panel.grid.minor = element_blank()

Remove facet_wrap labels completely

I'd like to remove the labels for the facets completely to create a sort of sparkline effect, as for the audience the labels are irrelevant, the best I can come up with is:
library(MASS)
library(ggplot2)
qplot(week,y,data=bacteria,group=ID, geom=c('point','line'), xlab='', ylab='') +
facet_wrap(~ID) +
theme(strip.text.x = element_text(size=0))
So can I get rid of the (now blank) strip.background completely to allow more space for the "sparklines"?
Or alternatively is there a better way to get this "sparkline" effect for a large number of binary valued time-series like this?
For ggplot v2.1.0 or higher, use element_blank() to remove unwanted elements:
library(MASS) # To get the data
library(ggplot2)
qplot(
week,
y,
data = bacteria,
group = ID,
geom = c('point', 'line'),
xlab = '',
ylab = ''
) +
facet_wrap(~ ID) +
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
In this case, the element you're trying to remove is called strip.
Alternative using ggplot grob layout
In older versions of ggplot (before v2.1.0), the strip text occupies rows in the gtable layout.
element_blank removes the text and the background, but it does not remove the space that the row occupied.
This code removes those rows from the layout:
library(ggplot2)
library(grid)
p <- qplot(
week,
y,
data = bacteria,
group = ID,
geom = c('point', 'line'),
xlab = '',
ylab = ''
) +
facet_wrap(~ ID)
# Get the ggplot grob
gt <- ggplotGrob(p)
# Locate the tops of the plot panels
panels <- grep("panel", gt$layout$name)
top <- unique(gt$layout$t[panels])
# Remove the rows immediately above the plot panel
gt = gt[-(top-1), ]
# Draw it
grid.newpage()
grid.draw(gt)
I'm using ggplot2 version 1 and the commands required have changed.
Instead of
ggplot() ... +
opts(strip.background = theme_blank(), strip.text.x = theme_blank())
you now use
ggplot() ... +
theme(strip.background = element_blank(), strip.text = element_blank())
For more detail see http://docs.ggplot2.org/current/theme.html
Sandy's updated answer seems good but, possibly has been rendered obsolete by updates to ggplot? From what I can tell the following code (a simplified version of Sandy's original answer) reproduces Sean's original graph without any extra space:
library(ggplot2)
library(grid)
qplot(week,y,data=bacteria,group=ID, geom=c('point','line'), xlab='', ylab='') +
facet_wrap(~ID) +
theme(strip.text.x = element_blank())
I am using ggplot 2.0.0.
As near as I can tell, Sandy's answer is correct but I think it's worth mentioning that there seems to be a small difference the width of a plot with no facets and the width of a plot with the facets removed.
It isn't obvious unless you're looking for it but, if you stack plots using the viewport layouts that Wickham recommends in his book, the difference becomes apparent.

heatmap ggplot2 color ramp (scale_fill_gradient)

I am trying to plot a heat map with ggplot2 and I would like to resize the colorbar and increase the font.
Here is the relevant part of the code:
g <- ggplot(data=melt.m)
g2 <- g+geom_rect(aes(xmin=colInd-1, xmax=colInd,
ymin=rowInd-1, ymax=rowInd, fill=value))
g2 <- g2+scale_x_continuous('beta', breaks=c(1, ceiling(cols/2), rows)-0.5,
labels=c(1,ceiling(cols/2), rows))
g2 <- g2+scale_y_continuous('alpha', breaks=c(1, ceiling(rows/2), rows)-0.5,
labels=c(1, ceiling(rows/2), rows))
g2 <- g2+opts(panel.grid.minor=theme_line(colour=NA),
panel.grid.major=theme_line(colour=NA),
panel.background=theme_rect(fill=NA, colour=NA),
axis.text.x=theme_text(size=30),
axis.text.y=theme_text(size=30, angle=90),
axis.title.x=theme_text(size=30),
axis.title.y=theme_text(size=30, angle=90), title = title)
heatscale <- c(low='ghostwhite', high='steelblue')
g2 <- g2+scale_fill_gradient("", heatscale[1], heatscale[2], bias = 10)
It works fine, the problem is that the color legend on the right side is too small. Is there a way to make the color legend bigger and increase the font size of the legend?
Thanks,
kz
We don't have your melt.m data, so the code you give is not reproducible. Using the diamonds dataset that comes with ggplot2 as an example, though:
ggplot(diamonds, aes(x=table, y=price)) +
geom_bin2d() +
scale_fill_gradient("", 'ghostwhite', 'steelblue', bias=10) +
opts(legend.key.width=unit(1, "in"),
legend.text = theme_text(size=30))
legend.key.width and legend.text are what you are looking for. I have used exaggerated sizes to make it more obvious.
For more details on the options available, see https://github.com/hadley/ggplot2/wiki/+opts%28%29-List
I tried this and found that R or ggplot2 have changed in the last four years. It yielded the error:
Error: 'opts' is deprecated. Use 'theme' instead. (Defunct; last used in version 0.9.1)
Was able to get it to work with the following instead:
p + theme(legend.text = element_text(size=30),legend.key.size = unit(1, "in"))
Initially tried just changing the text size but had to change the key size with it or it becomes unreadable. Also, unit needs a library explicitly loaded with library(grid)

Resources