如何用R绘制双坐标图?

Python014

如何用R绘制双坐标图?,第1张

library(plotrix)

twoord.plot(lx,ly,rx,ry,data=NULL,main="",xlim=NULL,lylim=NULL,rylim=NULL,

mar=c(5,4,4,4),lcol=1,rcol=2,xlab="",lytickpos=NA,ylab="",ylab.at=NA,

rytickpos=NA,rylab="",rylab.at=NA,lpch=1,rpch=2,

type="b",xtickpos=NULL,xticklab=NULL,halfwidth=0.4,axislab.cex=1,

do.first=NULL,xaxt="s",...)

xval1 <- seq.Date(as.Date("2017-01-02"), as.Date("2017-01-10"), by="day")

xval2 <- seq.Date(as.Date("2017-01-01"), as.Date("2017-01-15"), by="day")

going_up<-seq(3,7,by=0.5)+rnorm(9)

going_down<-rev(60:74)+rnorm(15)

twoord.plot(2:10,going_up,1:15,going_down,xlab="Sequence",

 ylab="Ascending values",rylab="Descending values",lcol=4,

 main="Plot with two ordinates - points and lines",

 do.first="plot_bg()grid(col=\"white\",lty=1)")

 axis.Date(1,xval2)

library(ggplot2)

library(gtable)

library(grid)

grid.newpage()

# two plots

p1 <- ggplot(mtcars, aes(mpg, disp)) + geom_line(colour = "blue") + theme_bw()

p2 <- ggplot(mtcars, aes(mpg, drat)) + geom_line(colour = "red") + theme_bw() %+replace% theme(panel.background = element_rect(fill = NA))

# extract gtable

g1 <- ggplot_gtable(ggplot_build(p1))

g2 <- ggplot_gtable(ggplot_build(p2))

# overlap the panel of 2nd plot on that of 1st plot

pp <- c(subset(g1$layout, name == "panel", se = t:r))

g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]], pp$t, pp$l, pp$b, pp$l)

# axis tweaks

ia <- which(g2$layout$name == "axis-l")

ga <- g2$grobs[[ia]]

ax <- ga$children[[2]]

ax$widths <- rev(ax$widths)

ax$grobs <- rev(ax$grobs)

ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")

g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)

g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)

# draw it

grid.draw(g)

用ggplot2画个双坐标也太难了吧。。。。

d <- data.frame(name=c("zhao","qian","sun","li"),weight=c(62,58,79,60),height=c(178,169,180,173))

x <- d$name

y1 <- d$weight

y2 <- d$height

bar <- barplot(y1,xlim=c(0,5),ylim=c(0,100),ylab="Weight",col="blue",col.axis="blue",col.lab="blue")

mtext(x,side=1,line=1,at=bar,col="black")

mtext("Name",side=1,line=3,col="black")

par(new=T)    ## 创建新的画布,类似于ggplot2中的图层叠加

plot(bar,y2,axes=F,xlim=c(0,5),ylim=c(100,190),xlab="",ylab="",col="red",type="b")

axis(4,col="red",col.ticks="red",col.axis="red")

mtext("Heigth (cm)",side=4,line=3,col="red")

若画图时需要将纵坐标方向改变,可以采用取相反数的方法。正常情况下,y轴是从下往上依次增大,可以让所有数值乘以-1,使y轴从下往上依次减小,然后通过axis函数修改y轴的label。

R语言学习  绘制双坐标轴(双y轴)的方法 plotrix_瞎掰大数据_新浪博客

利用ggplot2画双坐标轴曲线

1\加宽LIST的宽度,使其能容下最长的那个

2\使用list1.ToolTipText属性,当选中某个记录时,为list1.ToolTipText 属性赋值为这个记录的内容,这样鼠标移上去就会有一个说明,当然也可以用一个LABEL来显示.

3\用API函数

4\list控件一般只放比较短的字符串,您放这么长的干什么?调整您的程序设计.

API的使用

Option Explicit

Private Const LB_SETHORIZONTALEXTENT = &H194

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()

Dim max As Long, f As Font, i As Integer

Me.ScaleMode = vbPixels

Set f = Me.Font

Set Me.Font = List1.Font

With List1

For i = 0 To .ListCount

If Me.TextWidth(.List(i)) >max Then

max = Me.TextWidth(.List(i))

End If

Next

End With

max = max + 10

Set Me.Font = f

SendMessage List1.hwnd, LB_SETHORIZONTALEXTENT, max, ByVal 0&

End Sub

一、模拟数据

背景:绘制深圳市某区各街道10月份法定传染病发生率较上月的变化情况(数据均为虚拟)。

dat<-data.frame(grou=c("平湖街道","布吉街道","吉华街道","坂田街道","南湾街道", "横杠街道","园山街道","龙岗街道","龙城街道","宝龙街道","坪地街道"), val=c(32.12,-12.22,22.3,24.55,-27.8,-10.56,31.22,23.45,-22.3,-12.45,22.78))

(可左右滑动查看代码)

二、绘图

我们绘制一个基本的图形,并作基本修饰。

library(ggplot2) library(hrbrthemes) ggplot(dat, aes(x = grou, y = val)) + geom_bar(stat = "identity", show.legend = FALSE, width = .5) + xlab("街道") + ylab("变化率")+ theme_ipsum()+ labs( title = "深圳市某区10月各街道法定传染病较上月变化(%)", subtitle="R语言与医学生" )