R语言绘制块状直方图

library(ggplot2)
library(RColorBrewer)
library(scales)

x <- rnorm(250 , mean=10 , sd=1) #(a2) ??????̬?ֲ???ԭʼ????

#x <-sample(1:20, 250, replace = TRUE) #(a1) ???Ӿ??ȷֲ???ԭʼ????

step<-0.2
breaks<- seq(min(x)-step,max(x)+step,step)#"sturges"

hg <- hist(x, breaks = breaks , plot = FALSE) # Make histogram data but do not plot

bins <- length(hg$counts) # How many bin categories are needed?
yvals <- numeric(0)                  # A blank variable to fill in
xvals <- numeric(0) 
for(i in 1:bins) {                  # Start a loop
  yvals <- c(yvals, hg$counts[i]:0)  # Work out the y-values
  xvals <- c(xvals, rep(hg$mids[i], hg$counts[i]+1))  # Work out x-values
}    # End the loop
                                                   # End the loop
dat <- data.frame(xvals, yvals)  # Make data frame of x, y variables
dat <- dat[yvals > 0, ]          # Knock out any zero y-values

colormap <- colorRampPalette(rev(brewer.pal(11,'Spectral')))(32)

#------------------------------------??״tile---------------------------------------
ggplot(dat, aes(x=xvals,y=yvals,fill=yvals))+
  geom_tile(colour="black")+
  scale_fill_gradientn(colours=colormap)+
  ylim (0, max(yvals)*1.3)+
  theme(
    text=element_text(size=15,color="black"),
    plot.title=element_text(size=15,family="myfont",face="bold.italic",hjust=.5,color="black"),
    legend.background = element_blank(),
    legend.position=c(0.9,0.75)
  )

在这里插入图片描述

#-----------------------------------ԲȦpoint-----------------------------------------
ggplot(dat, aes(x=xvals,y=yvals,fill=yvals))+
  geom_point(colour="black",shape=21,size=4)+
  scale_fill_gradientn(colours=colormap)+
  ylim (0, max(yvals)*1.3)+
  theme(
    text=element_text(size=15,color="black"),
    plot.title=element_text(size=15,family="myfont",face="bold.italic",hjust=.5,color="black"),
    legend.background = element_blank(),
    legend.position=c(0.9,0.75)
  )

在这里插入图片描述
开发工具:RStudio与微信Alt+A截屏工具
参考文献:https://download.csdn.net/download/weixin_39840588/11515554?utm_medium=notify.im.downloadSource.a&username=m0_38127487

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值