How to Suppress the Vertical Gridlines in a Ggplot2 Plot

How can I suppress the vertical gridlines in a ggplot2 plot?

Try using

scale_x_continuous(breaks = NULL)

This would remove all the vertical gridlines as well as x-axis tickmark labels.

Remove specific vertical gridline from ggplot

Try using panel.grid.minor.x

library(ggplot2)

ggplot(diamonds) +
aes(x = price) +
geom_histogram(
color = 'white',
fill = 'blue4'
) +
theme_minimal() +
theme(panel.grid.minor.x = element_blank())

How can I suppress the vertical gridlines in a ggplot2 plot while retaining the x-axis labels?

As code in comments does not display nicely, so I am posting this as an answer. You could do something like this and add labels manually with geom_text():

ggplot(data, aes(x, y)) +
geom_bar(stat = 'identity') +
scale_x_continuous(breaks = NA) +
opts(
panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
panel.grid.minor = theme_blank(),
panel.background = theme_blank(),
axis.ticks = theme_blank()
)+
geom_text(aes(label = x, y = -.3))

How to remove gridlines at specific values using ggplot2?

This could be achieved by setting restricting the range of the minor breaks too:

library(dplyr)
library(ggplot2)

data.frame(val1 = seq(0, 1, 0.1),
val2 = seq(0, 1, 0.1)) %>%
ggplot(aes(x = val1, y = val2)) +
scale_x_continuous(limits = c(0, 1.05),
breaks = seq(0, 1, 0.1),
minor_breaks = seq(0, 1, 0.05))

Sample Image

Created on 2021-06-07 by the reprex package (v2.0.0)

How to surpress horizontal grid lines without removing ticks from y axis?

You were 95% of the way there. The grid has two sets of lines--major and minor. You removed half of the horizontal grid (panel.grid.minor.y). To remove the other half add panel.grid.major.y = element_blank(). To add ticks to both the x and y axis add axis.ticks = element_line()

df <- data.frame("prop" = c(102.73,260.65), "Name" = c("All Genes","RG Genes"))

p <- ggplot(data = df, aes(x = Name, y = prop, fill = Name)) +
geom_bar(stat = "identity") +
labs(x = "", y = "Proportion of cis EQTLs") +
scale_fill_brewer(palette="Greens") +
theme_minimal() +
theme(legend.position = "none",
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.line = element_line(),
axis.ticks = element_line())

p + annotate("text", x = 1.5, y = 280, label = "p = XXXXXX", size = 3.5) +
annotate("rect", xmin = 1, xmax = 2, ymin = 270, ymax =270, alpha=1,colour = "black")

Sample Image

How do I adjust visibility of gridlines in ggplot chart

This could be achieved by switching to a lighter color or by reducing the opacity of the color used for the grid lines which both could be achieved via theme option panel.grid. Below I show the second approach. Unfortunately element_line has no alpha argument to set the opacity but you could adjust it via the hex color code:

  1. I make use of the default grid line color "grey92" which has rgb values (235, 235, 235).

  2. To set the opacity I use rgb() which as a fourth argument takes the opacity or alpha which I reduce to a value of 100:

library(ggplot2)
library(dplyr)

data = mtcars

# Reduce the opacity of the grid lines: Default is 255
col_grid <- rgb(235, 235, 235, 100, maxColorValue = 255)

data %>%
select(mpg, disp) %>%
ggplot(aes(disp, mpg))+
geom_point(size = 3)+
theme_bw() +
theme(panel.grid = element_line(color = col_grid))

Sample Image

Can't remove gridlines when plotting with geom_sf

This issue was raised on the ggplot2 github site. You can remove the gridlines by either of:

  1. Setting the colour of the gridlines to be transparent with theme(panel.grid.major = element_line(colour = "transparent"))

  2. Adding coord_sf(datum = NA) after calling geom_sf

How to control number of minor grid lines in ggplot2?

You do it by explicitly specifying minor_breaks() in the scale_x_continuous. Note that since I did not specify panel.grid.major in my trivial example below, the two plots below don't have those (but you should add those in if you need them). To solve your issue, you should specify the years either as a sequence or just a vector of years as the argument for minor_breaks().

e.g.

 ggplot(movies, aes(x=rating)) + geom_histogram() + 
theme(panel.grid.minor = element_line(colour="blue", size=0.5)) +
scale_x_continuous(minor_breaks = seq(1, 10, 1))

Sample Image

 ggplot(movies, aes(x=rating)) + geom_histogram() + 
theme(panel.grid.minor = element_line(colour="blue", size=0.5)) +
scale_x_continuous(minor_breaks = seq(1, 10, 0.5))

Sample Image

Eliminate vertical lines ggplot

Try this, redifining guide_grid.

This solution is from the Cookbook for R

# Save the original definition of the guide_grid
guide_grid_orig <- ggplot2:::guide_grid

# Create the replacement function
guide_grid_no_vline <- function(theme, x.minor, x.major, y.minor, y.major) {
x.minor <- setdiff(x.minor, x.major)
y.minor <- setdiff(y.minor, y.major)

ggname("grill", grobTree(
theme_render(theme, "panel.background"),
if(length(y.minor) > 0) theme_render(
theme, "panel.grid.minor", name = "y",
x = rep(0:1, length(y.minor)), y = rep(y.minor, each=2),
id.lengths = rep(2, length(y.minor))
),
if(length(y.major) > 0) theme_render(
theme, "panel.grid.major", name = "y",
x = rep(0:1, length(y.major)), y = rep(y.major, each=2),
id.lengths = rep(2, length(y.major))
)
))
}
# Set the environment to be the same as original
environment(guide_grid_no_vline) <- environment(ggplot2:::guide_grid)

# Assign the function inside ggplot2
assignInNamespace("guide_grid", guide_grid_no_vline, ns="ggplot2")

# Draw the plot with the redefined guide_grid
ggplot(CO3, aes(x=outcome)) +
geom_bar(aes(x=outcome))+
facet_grid(Treatment~Type, margins='Treatment', scales='free') +
theme_bw() +
opts(axis.text.x=theme_text(angle= 45, vjust=1, hjust= 1))

# Restore the original guide_grid function so that it will draw all gridlines again
assignInNamespace("guide_grid", guide_grid_orig, ns="ggplot2")


Related Topics



Leave a reply



Submit