Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 无法在ggplot2中绘制正确的x轴_R_Ggplot2 - Fatal编程技术网

R 无法在ggplot2中绘制正确的x轴

R 无法在ggplot2中绘制正确的x轴,r,ggplot2,R,Ggplot2,我使用R中的ggplot2绘制以下数据 dat<-structure(list(Month = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L), grp1 = st

我使用R中的ggplot2绘制以下数据

dat<-structure(list(Month = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 
8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
12L, 12L, 12L, 12L), grp1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 
4L, 1L, 2L, 3L, 4L), .Label = c("(-Inf,2]", "(2,7]", "(7,14]", 
"(14, Inf]"), class = "factor"), n = c(71L, 59L, 36L, 10L, 55L, 
73L, 18L, 10L, 97L, 82L, 22L, 5L, 120L, 79L, 15L, 2L, 140L, 62L, 
15L, 174L, 60L, 11L, 188L, 71L, 2L, 183L, 53L, 2L, 211L, 50L, 
2L, 171L, 69L, 7L, 1L, 98L, 85L, 13L, 6L, 72L, 62L, 24L, 9L)), class    
= "data.frame", row.names = c(NA,-43L))

dat如果您希望沿X轴输入月份名称,则可以将
as.factor(Month)
添加到ggplot脚本中。举个例子:-

p<-ggplot(data=dat,aes(as.factor(Month), n, fill = grp1))
p<- p +  geom_col()
p <- p + theme(panel.background=element_rect(fill="white"),
               plot.margin = margin(0.5,0.5,0.5,0.5, "cm"),
               panel.border=element_rect(colour="black",fill=NA,size=1),
               axis.line.x=element_line(colour="black"),
               axis.line.y=element_line(colour="black"),
               axis.text=element_text(size=20,colour="black",family="sans"),
               axis.title=element_text(size=20,colour="black",family="sans"),
               legend.position = "right", legend.key = element_rect(fill = 'white'))
p <- p + scale_y_continuous(limits = c(0,300),breaks=c(seq(0,300,50)), expand=c(0,0))
p <- p + scale_x_discrete(breaks=c(seq(1,12,1)),labels=c("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"),expand=c(0,0))
p <- p + labs(x = "Month", y = "Number of Days")
p

p如果您希望在xaxis上显示月份名称,那么可以将
as.factor(Month)
添加到您的ggplot脚本中。举个例子:-

p<-ggplot(data=dat,aes(as.factor(Month), n, fill = grp1))
p<- p +  geom_col()
p <- p + theme(panel.background=element_rect(fill="white"),
               plot.margin = margin(0.5,0.5,0.5,0.5, "cm"),
               panel.border=element_rect(colour="black",fill=NA,size=1),
               axis.line.x=element_line(colour="black"),
               axis.line.y=element_line(colour="black"),
               axis.text=element_text(size=20,colour="black",family="sans"),
               axis.title=element_text(size=20,colour="black",family="sans"),
               legend.position = "right", legend.key = element_rect(fill = 'white'))
p <- p + scale_y_continuous(limits = c(0,300),breaks=c(seq(0,300,50)), expand=c(0,0))
p <- p + scale_x_discrete(breaks=c(seq(1,12,1)),labels=c("JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"),expand=c(0,0))
p <- p + labs(x = "Month", y = "Number of Days")
p

p谢谢你。不过有一个问题。为什么它没有出现在原始情节中。我正在绘制另一个类似的数据(相同格式),但不需要as.factor。只有在这种情况下,它才不起作用。我认为
scale\u x\u discrete
是问题所在,因为x轴标签一运行就消失了。这是因为月份被设置为整数(同样的问题也发生在数字的时候)。如果将其转换为系数,则在运行时会保留xaxis标签
scale\u x\u discrete
谢谢。不过有一个问题。为什么它没有出现在原始情节中。我正在绘制另一个类似的数据(相同格式),但不需要as.factor。只有在这种情况下,它才不起作用。我认为
scale\u x\u discrete
是问题所在,因为x轴标签一运行就消失了。这是因为月份被设置为整数(同样的问题也发生在数字的时候)。如果将其转换为系数,则在运行
scale\u x\u discrete