I always spot a handful of strange white horizontal dotted lines that seem to appear on the borders between vertically adjacent hexagons on every hexbin density plot that I create using ggplot2 in the R environment. They certainly do not belong there... This behavior (bug) is reproducible on several R installations on different computers with different MS Windows versions and different graphics drivers. I don't have a clue on how to get rid of these dotted lines.
Below is a plot showing the problem, and the code that produced the plot.
Is this a known problem? Any hint to overcome this? Thanks a bunch for any help!
## Load libraries
library(ggplot2)
library(hexbin)
## Create some data
set.seed(222)
myx <- data.frame(x = 0.4 + rnorm(10000, mean = 0, sd = 0.1))
myy <- data.frame(y = 0.5 + rnorm(10000, mean = 0, sd = 0.1))
MyData <- data.frame(xvariable=myx$x*100, yvariable=myy$y*100)
## Prepare the plot window
dev.off(2)
windows.options(width=6, height=6)
op <- par(mfrow = c(1,1))
op <- par(oma = c(0,0,0,0) + 0.1,
mar = c(5.1, 5.1, 4.1, 2.1))
## Creaate the plot
ggplot(MyData, aes(x=xvariable, y=yvariable) ) +
geom_hex(bins=66) +
xlim(0, 100) +
ylim(0, 100) +
ggtitle("My plot") +
scale_fill_continuous(type = "viridis") +
theme_bw() +
theme(plot.title = element_text(color="black", size=17, face="bold"),
axis.title.x = element_text(color="black", size=17, face="bold"),
axis.title.y = element_text(color="black", size=17, face="bold"),
axis.text=element_text(color="black", size=15, face="bold"),
legend.title = element_text(color = "black", size = 15),
legend.text = element_text(color = "black", size=12),
legend.position = c(0.9,0.2), legend.direction = "vertical")
Looks like if you save as pdf and then convert that to image, there will be no line.
I have this issue too, RStudio version 1.4.1717 "Juliet Rose" (df86b69e, 2021-05-24) for Windows. Looks like white lines going across the 'Plots' tab if I use geom_hex(). If I resize the output window by a pixel at a time (vertically) , the lines will disappear.
Maybe there's too many hex bins to begin with and the resolution causes gaps every so often.
before
after
Related
(Edit: now submitted as ggplot2 issue #4183.)
I'm not sure exactly what conditions cause this to happen, but here's a pretty small example derived from the real plot that I was working on when I ran into this:
library(ggplot2)
d = data.frame(x = seq(0, 2, len = 2500))
d$y = d$x^2
ggsave("~/plot.png", width = 3, height = 4, dpi = 90,
ggplot(d) +
geom_col(aes(x, y), width = max(diff(d$x))) +
theme_bw() +
theme(
panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()))
With ggplot2 3.3.2 on R 3.6.2 on Linux, I get this:
How do I reliably avoid getting vertical white lines like the one at about x = 0.8 above?
ggplot has decreed this a won't-fix, but it can be worked around by increasing the width with something like geom_col(aes(x, y), width = max(diff(d$x)) * 1.05, position = "identity"):
I am aligning line graphs in a panel. First one is slightly off compared to the other two (see figure). I have similar problems with the same kind of graph in other cases (in the other cases, the graph in question is the second one, and in all cases it is labeled as "study 2). I am using ggplot 2:
Setting up data & theme:
library(ggplot2)
# common theme used by all plots
theme1 <- theme(plot.title = element_text(hjust = 0.5, size = 20, face = "italic"),
legend.position = c(0.5,0.2),
legend.text=element_text(size=10, face="bold", color="black"),
legend.title = element_blank(),
legend.key.size = unit(2.5, "line"),
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, face = "bold", size=15, color="black"),
panel.background = element_rect(fill = "white"),
axis.title.x=element_blank(),
axis.title.y = element_blank(),
axis.ticks.length = unit(0.2, "cm"),
axis.text.y = element_text(size = 18, colour="black"),
panel.border = element_rect(colour = "black", fill=NA, size=3),
panel.grid.major = element_line(size=0.2, colour = "gray", linetype="solid"),
panel.grid.minor.y =element_line(size=0.2, colour = "gray", linetype="solid"),
aspect.ratio = 4,
plot.margin = unit(c(0,0,0,2),"cm"))
# data for first plot
N300 <- data.frame(evaluation=rep(c("Low\nSalary\n($20k)", "High\nSalary\n($50k)"),
each = 2),
values =c(4.56, 4.20, 5.12, 4.55),
Condition =rep(c("High Ratio (200:1)", "Low Ratio (50:1)")))
# data for second plot
dfie2 <- data.frame(values = c(3.86, 3.94, 3.64),
condition = c("Baseline (300:1)", "CEO Lower (100:1)", "Median higher (100:1)"))
# data for third plot
dfie3 <- data.frame(evaluation=rep(c("Joint", "Separate"), each = 3),
values = c(5.03, 4.24, 4.43, 5.05, 5.21, 4.97),
Condition = rep(c("Baseline (400:1)", "CEO Lower (160:1)", "Median Higher (160:1)")))
Plot:
# first plot
Oldstudy <- ggplot(data=N300, aes(x=Condition, y=values, group=evaluation, label=values)) +
ggtitle("Study 2")+
geom_line(aes(linetype=evaluation), size=1.5) +
geom_point(shape=20, size=5) +
scale_y_continuous(breaks=c(1,2,3,4,5,6,7), limits=c(1,7))+
scale_linetype_manual(values=c("solid", "dotdash")) +
theme1
# second plot
France <- ggplot(data=dfie2, aes(x=condition, y=values, group=1)) +
ggtitle("Study 3")+
geom_line(size=1.5)+
#geom_text(aes(label=values), vjust=2, fontface="bold", size=5)+
geom_point(shape=20, size=5)+
scale_y_continuous(breaks=c(1,2,3,4,5,6,7), limits=c(1,7))+
theme1
# third plot
JS <- ggplot(data=dfie3, aes(x=Condition, y=values, group=evaluation)) +
ggtitle("Study 4")+
geom_line(aes(linetype=evaluation), size=1.5)+
geom_point(shape=20, size=5)+
scale_y_continuous(breaks=c(1,2,3,4,5,6,7), limits=c(1,7))+
scale_linetype_manual(values=c("solid", "dotted"))+
theme1
#### combine graphs ####
library(grid)
library(gridExtra)
grid.arrange(Oldstudy, France, JS, nrow = 1,
top = textGrob("Support for Lowering CEO compensation",
gp = gpar(fontface = "bold", fontsize = 20)
))
This is the plot that the code generates:
It looks like the three plots are being aligned by their middle points, which is causing the first one to look misaligned as it has shorter X-axis labels than the other two.
Instead of using the grid.arrange() function, you could try using the plot_grid() function of the cowplot package instead, which should align the plots by their axes instead of centring each one.
(Please note that I've done a considerable amount of cleaning / steamlining on the code included in your question. You are, of course, free to roll back the edit if you feel it misrepresents your problem, but I second what #camille mentioned in her comment that your original code had too many unnecessary details. While terse code can be hard to read, & comments help a reader understand what a piece of code is trying to do, peppering EVERY line with comments is generally a step too far.)
library(cowplot)
# just the plots, arranged horizontally
joint.plot <- plot_grid(Oldstudy, France, JS, nrow = 1, align = "h", axis = "tb")
# define joint title
plot.title <- ggdraw() +
draw_label("Support for Lowering CEO compensation",
fontface = 'bold', size = 20)
# combine the title & the joint plots vertically
plot_grid(plot.title, joint.plot,
ncol = 1,
rel_heights = c(0.1, 1)) # adjust relative height of title vs plot as needed
I am generating bubble charts from NBA shot data clusters. The final form of the data is:
Where Group.1 is the index of the cluster, ad.SHOT_MADE_FLAG is the field goal percent for the cluster, coords.x1 and x2 are the mean x and y coordinates of the points in that cluster, and x is the number of shots (x and y points) in that cluster.
I am plotting the data with the following:
courtImg.URL <- "https://thedatagame.files.wordpress.com/2016/03/nba_court.jpg"
court <- rasterGrob(readJPEG(getURLContent(courtImg.URL)),
width=unit(1,"npc"), height=unit(1,"npc"))
p6 <- ggplot(final, aes(x = final$coords.x1, y = final$coords.x2, size =
final$x,fill=final$ad.SHOT_MADE_FLAG)) +
geom_point(shape = 21) +
annotation_custom(court, -250, 250, -52, 418) +
scale_x_continuous() +
coord_fixed() +
scale_fill_gradientn(colours = c("Blue","Red")) +
theme(line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
plot.title = element_text(size = 17, lineheight = 1.2, face = "bold")) +
ggtitle("Stephen Curry Shot Chart")
p6
This outputs the following chart
I am wanting to solve two issues with this. First the background image is covering up the majority of the data. Second, I want to only show the plot below the 418 point on the y axis. I dont want to show shots from the backcourt as they aren't as relevant. Just for reference, when I remove the annotation_custom() line, it shows the following plot:
So the implementation of the annotation_custom line appears to be part of the problem. Any help would be greatly appreciated. Thanks!
ggplot2 draws plot layers in the order you specify them. To move the image of the court below the points, put it first in the drawing order. The other fix that might make your plot a little nicer is to make the panel background transparent so that you can see the points on top of the image, which I assume is what you're going for.
You can set the ends of the plots using the limits argument in scale_y_continuous().
Updated plotting code:
p6 <- ggplot(final, aes(x = final$coords.x1, y = final$coords.x2, size =
final$x,fill=final$ad.SHOT_MADE_FLAG)) +
annotation_custom(court, -250, 250, -52, 418) +
geom_point(shape = 21) +
scale_x_continuous() +
scale_y_continuous(limits=c(-52,418)) +
coord_fixed() +
scale_fill_gradientn(colours = c("Blue","Red")) +
theme(line = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.title = element_blank(),
panel.background = element_rect(fill="transparent"),
plot.title = element_text(size = 17, lineheight = 1.2, face = "bold")) +
ggtitle("Stephen Curry Shot Chart")
p6
enter image description hereI'm reading in a csv file and using ggplot to make a box plot and having issues with the size of the text on the x axis. I can't reduce the text size because I am reducing the over all image for a manuscript and this is the optimal size to make the axes visible. I'd like to have the genotype names below each box plot on the x axis, I've angled genotype names on the axis, but it still doesn't seem to be enough and looks a bit awkward. I'd like to add a line break for at least the longest genotype (4-67/Chrimson), but I don't think I am doing this right. I've tried setting new labels using in the scale_x_discrete and also in the axis.text.x but under both conditions, it either skips over that variable or ignores the command.
FYI: the data frame has two variables, group activity and genotype and genotype has 4 levels.
Thank you in advance!!
Here's my code so far..
dat5=read.csv("Time Point 120 Raw Data C-Test.csv")
plot5<- ggplot(dat5, aes(x = Genotype, y = Group.Activity, col = Genotype, fill = Genotype, ymin= -40, ymax = 50)) +
geom_boxplot(fatten = 1, lwd = .5, alpha = .6) +
# reorder so x-axis is not alphabetical
scale_x_discrete(limits=c("4-67/Chrimson","4-67/+","+/Chrimson", "+/+"))+
scale_fill_manual(values=c("#F99205", "#4ED55F", "#36A6D6", "#5752D0"))+
scale_color_manual(values=c("#F99205", "#4ED55F", "#36A6D6", "#5752D0"))+
geom_beeswarm(size = 1.5, alpha = .75, cex = 3)+
ylab("Percent Activity After Baseline Subtraction")+
theme_classic()+
theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 16, color="black"), axis.ticks.x = element_blank())+
theme(axis.text.y = element_text(size = 24, color="black"), axis.ticks.y = element_blank())+
theme(axis.title.y = element_text (size = 24, color="black"))+
theme(axis.ticks.x = element_blank())+
theme(legend.position = "none")
I am using the R package cooccur and cannot figure out how to change the font size in the associated graphics. The par() method does not seem to work.
Here is the example given by the package:
data(finches)
cooccur.finches <- cooccur(mat=finches,
type="spp_site",
thresh=TRUE,
spp_names=TRUE)
plot(cooccur.finches)
I am trying to change the font size of the species, the title and the legend to no avail on the heat map that is produced. Any help would be MUCH appreciated. Thanks!
Unfortunately the author didn't use defined theme inside the function, so if you want to not mess up the other customizations in place, this should work:
p <- plot(cooccur.finches)
p + theme_bw(base_size = 28) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(vjust = -4, face = "bold"),
panel.background = element_rect(fill = "white", colour = "white"),
panel.grid = element_blank()
legend.position = c(0.9, 0.5))
You can also use this code to set the size of the legend or title independently, e.g.
p + theme(plot.title = element_text(vjust = -4, face = "bold", size = 36))
Most unfortunately, this won't change the size of the species labels because they are set with geom_text(). To alter them, you'll have to hack the function yourself cooccur:::plot.cooccur. You only need to modify the last line:
p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0,
angle = -22.5)
# change to
p + geom_text(data = dfids, aes(label = X1), hjust = 1, vjust = 0,
angle = -22.5, size = 24)
It is a ggplot2 plot not a base one. So par will not work.
p <- plot(cooccur.finches)
p + theme(text = element_text(size = 10)) ## change text font size
or
p + theme_grey(base_size = 18) ## chnage all font size.
Author of Cooccur here. Sorry for the hassle with the text sizes being hard to adjust. I will deal with this when I get a chance.
Not a permanent solution, but easier than changing the function each time for the species labels, is to just directly re-assign the value in the ggplot object:
p$layers[[2]]$geom_params$size <- 10
Hope that helps. I might be a bit late to the scene...