简体   繁体   中英

How to customize bin graph in R

I am trying to customize some of the graph I am creating with R. The code I'm using looks something like this one

c1<-runif(5000, min=0, max=100)
c2<-sample(1:12, 5000, replace=TRUE)

dd<-data.frame(c1,c2)
g<-ggplot(dd, aes(c2, c1)) + 
    geom_bin2d(binwidth=c(2,5)) + 
    scale_fill_gradientn(colors=c("yellow", "red", "black")) +
    xlim(-1,13) + ylim(-5,55)
g

来自代码示例的绘制图

How can I make so that the graph is contained between 0 and 12 and 0 and 50 (I tried to set these value but it cut off a portion of the bin, but it's very ugly to see this huge grey border around the graph)? I need essentially to have a white background (and I know how to do it) with the axis in black and adjacent to the actual plot. Is there also a way to "smooth" the color gradient between each bin? The only way I know how to do it is reducing the number of bins but I need exactly those numbers.

Try this:

 g + scale_x_continuous(expand=expansion()) + scale_y_continuous(expand=expansion())

在此处输入图片说明

Check the documentation for scale_x_continuous , section for expand and

expansion : expand: For position scales, a vector of range expansion constants used to add some padding around the data to ensure that they are placed some distance away from the axes. Use the convenience function 'expansion()' to generate the values for the 'expand' argument. The defaults are to expand the scale by 5% on each side for continuous variables, and by 0.6 units on each side for discrete variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM