用R中的因子绘制线图 [英] Plotting line graph with factors in R
问题描述
然而,正如你所看到的,我得到了这些实线而不是漂亮的线条。这是我用来制作这张图的代码:
par(mar = c(5,5,2,5))
with(bedtimes,plot(trap,funestus,type =l,col =red3,
ylab = expression(italic(p))),
ylim = c(0 ,3))
par(new = T)
(bedtimes,plot(陷阱,床,pch = 16,轴= F,xlab = NA,ylab = NA,cex = )
轴(side = 4)
mtext(side = 4,line = 3,'bed in bed')
多边形(bedtimes $ bed,col = transp(gray ,0.3),border = NA)
这里是我正在使用的数据的输入:
(已移除)
我意识到这是因为我的x轴是一个因素而不是数字。然而,试图改变这种情况(例如使用 as.POSIXct(paste0(2016-07-12,bedtimes $ trap))
会导致各种问题,如我必须确保R通过使用 bedtimes $ trap < - factor(bedtimes $ trap,levels = bedtimes $ trap)
我怎样才能产生这个相同的图形,但用线条而不是这些破折号?
我最终想要一张看起来类似于这个图形的图表,完全一样):
谢谢!
正如我在评论中所说的,您可以尝试使用您的因子变量的水平:
par(mar = c(5,5,2,5))
with (bedtimes,plot(as.numeric(trap)),funestus,type =l,col =red3,
ylab = expression(italic(p))),
ylim = c(0 ,3))
par(new = T)
with(bedtimes,p (as.numeric(trap),bed,type =l,axes = F,xlab =,ylab = NA,cex = 1.2))
axis(4)
axis = 1,at = 1:长度(水平(bedtimes $ trap)),水平(bedtimes $ trap))
mtext(side = 4,line = 3,'比例在床上')
多边形bedtimes $ bed,col =gray,border = NA)
I am working on this graph in R:
However as you see I am getting these solid dashes instead of nice lines. Here is the code I used to make this graph:
par(mar = c(5,5,2,5))
with(bedtimes, plot(trap, funestus, type="l", col="red3",
ylab=expression(italic(p))),
ylim=c(0,3))
par(new = T)
with(bedtimes, plot(trap, bed, pch=16, axes=F, xlab=NA, ylab=NA, cex=1.2))
axis(side = 4)
mtext(side = 4, line = 3, 'Proportion in bed')
polygon(bedtimes$bed,col=transp("gray", 0.3), border = NA)
And here is the dput of the data I am using:
(removed)
I realise that this is occurring because my x axis is a factor and not numeric. However, trying to change this (e.g. using as.POSIXct(paste0("2016-07-12",bedtimes$trap))
is causing me all sorts of problems, as I had to ake sure R plotted these factors in the correct order originally by using bedtimes$trap <- factor(bedtimes$trap, levels = bedtimes$trap)
How can I produce this same graph but with lines instead of these dashes? I eventually want a graph that looks similar to this, to give you an idea (though not exactly the same):
Thank you!
As I said in comments, you can try to use the levels of your factor variable:
par(mar = c(5,5,2,5))
with(bedtimes, plot(as.numeric(trap), funestus, type="l", col="red3",
ylab=expression(italic(p))),
ylim=c(0,3))
par(new = T)
with(bedtimes, plot(as.numeric(trap), bed, type="l", axes=F, xlab="", ylab=NA, cex=1.2))
axis(4)
axis(side = 1, at=1:length(levels(bedtimes$trap)), levels(bedtimes$trap))
mtext(side = 4, line = 3, 'Proportion in bed')
polygon(bedtimes$bed,col="gray", border = NA)
这篇关于用R中的因子绘制线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!