rplotvisualizationcontourf

How to suppress colorbar in contour plots when using GGPlot in R


I am attempting to make a set of contour plots to show density functions using the ggplot library in r. These contour plots will all be normalized, and so the colorbars that GGPlot produces by default are unnecessary and clutter the image. I am looking for a way to suppress these colorbars.

Here is a basic working example:

#Some randomly generated data, using xnorm <- rnorm(100, 100, 2) and ynorm <- rnorm(100, 100, 1)  
xnorm <- c(100.63747,  99.61373, 102.78140,  97.55596,  99.73000,  97.48554, 101.09937, 102.27442,  98.44345,  98.63937, 95.18071, 100.99034, 100.25796,  99.85286, 101.37154, 103.62904,  98.52518,  99.29083, 98.43506, 100.04028, 100.50882, 101.83978,  97.61325,  99.25491,  97.67031, 100.76768, 101.15345,  99.45239,  99.00706,  99.25894, 101.67204,  98.53558, 103.28598,  99.37674,  98.29560, 102.57874, 101.78405,  96.64773,  98.57429, 102.58449, 102.34099, 101.76433,  99.42075,  96.68118,  98.21205,  98.60703, 103.40764, 100.38115, 101.02439,  96.77698,  96.95160,  98.77218, 100.74975,  96.60388, 100.82192, 100.31073,  98.66408,  96.43205, 101.66969,  99.22321, 100.76002,  96.41930,  99.01634,  96.66087, 100.78227,  95.19014,  98.61710,  98.12931, 101.02011,  99.38666, 104.99480, 100.88542, 100.12007,  97.54670,  98.56362,  98.38221,  98.01062,  99.57980,  99.79977, 101.61598,  99.70361,  98.49786,  97.03384,  98.88931,  96.49175, 101.65014,  98.50976, 102.70204, 103.10644, 100.1625, 101.50492 , 96.78505,  97.64607,  96.23355, 100.83890, 103.89213,  96.55422,  98.14217,  97.33865, 103.09313) 
ynorm <- c(101.00065,  99.01183, 101.03233,  99.87815, 101.42570,  98.62362,  98.75669,  99.11513,  98.08216,  99.93417,  99.55935,  98.59495, 100.56597,  99.82176, 100.28809, 101.41834, 101.15464, 100.27552, 98.47943,  99.26038, 100.03685, 101.29748, 100.78959, 98.89097, 101.30694, 100.49280,  98.63089, 100.79754, 100.14726, 101.13723,  99.52545,  98.15186,  98.04923, 100.32256,  99.40237,  99.29262, 100.35762,  98.66520, 100.22026,  99.13309,  98.59964, 101.88812,  99.92022, 100.63701, 100.05824, 100.99503,  98.82681,  99.17443, 100.80232,  99.28913, 100.57291, 100.37855,  99.84050,  98.30520, 99.69184,  98.44987,  99.44457,  99.44025,  98.90528,  98.95814,  99.95515,  99.79427,  99.53203,  99.08064, 100.30757,  98.70576,  96.13955, 100.01945, 100.54303, 101.35302,  99.91294,  98.85087, 101.03512, 100.74202,  99.40307, 100.08632, 100.26612, 101.96658,  98.12320,  99.77385,  99.97723, 100.41529,  99.99487,  99.56625,  97.87832, 101.47635, 101.01801,  99.67586,  99.17761, 100.50633, 99.61379, 101.79323, 100.37171, 100.58981,  99.44867, 100.62373,  99.69169, 100.65622,  99.91878,  99.65549) 

cplot <- ggplot(mapping = aes(x = xnorm, y = ynorm)) + 
geom_density_2d_filled(aes(fill = after_stat(level)), breaks = seq(0.1, 1, length.out = 10), contour_var = 'ndensity') + 
geom_density_2d(contour_var = 'ndensity')
print(cplot)

This code yields the following plot: Contour plot generated with GGPlot.  I would like to get rid of the colorbar This contour plot looks more or less as I would like, but I would like to remove the colorbar that shows the normalized levels. Any simple way to do this? Looking through ggplot's documentation didn't yield anything that I understood.

Thanks for the help!

EDIT: In the original post, I incorrectly referred to the contour plot's legend as a colorbar. The post below corrects my mistake, and provides an answer to my question. I am leaving the original, incorrect phrasing so that anyone else who is similarly confused can find this post.


Solution

  • You want to remove the "color bars" from a geom_density_2d_filled() plot. By this I think you mean the plot legend. If that is correct, then the following removes the legend. All you need is theme(legend.position = "none"). If the color bars are something else, then I'm not sure.

    cplot <- ggplot(mapping = aes(x = xnorm, y = ynorm)) + 
    geom_density_2d_filled(aes(fill = after_stat(level)), breaks = seq(0.1, 1, length.out = 10), contour_var = 'ndensity') + 
    geom_density_2d(contour_var = 'ndensity')+
      theme(legend.position = "none")
    print(cplot)