问题描述
首先,我对R和stackoverflow还是很陌生,因此对任何格式问题深表歉意。我正在尝试分别绘制许多人的图,但是当我使用facet_wrap
时,图太多了,所以它们都很小,看不到。我切换到facet_wrap_paginate
,并且绘图几乎是完美的……除了缺少x轴标签!
我的代码(使用ggplot2和ggforce):
library(ggplot2)
library(ggforce)
lb <- c("8AM","2PM","8PM","2AM","8AM")
L <- 1440
xat <- c(1,L/4,L/2,3*L/4,L)
Baseline1 <- ggplot(Baseline.df_long,aes(x = Minute,y = value,color = key,group = key)) +
facet_wrap_paginate(~ key,ncol = 3,nrow = 3,page = 1,scales = "free_x") +
geom_bar(stat = "identity") +
scale_y_continuous(breaks=seq(0,2500,500),limits =c(0,2500)) +
scale_x_discrete(name="Time",breaks=c(xat),labels=c(lb)) +
labs(title ="Piglet Activity",y = "Activity") +
theme_bw(base_size = 14) +
theme(legend.position = "none")
我尝试过使用scales = "free x"
和不使用Baseline.df_long
的情况,我也尝试过固定的方法。这些都没有改变。当前缺少x轴标签的情况是这样的:
这就是我希望标签显示的样子,并且我能够使用带有facet_wrap的单个列来为单个个人做到这一点:
作为参考,所以您知道这是什么样的数据集,structure(list(Minute = 1:50,key = c("Pig_03","Pig_03","Pig_03"),value = c(0L,0L,165L,23L,12L,81L,47L,105L,70L,0L)),row.names = c(NA,-50L),class = "data.frame")
看起来像这样,但是显然有更多的数据
class ModelFactory {
fun setA() : ModelFactory {
// blabla...
}
fun setB() : ModelFactory {
// blabla...
}
fun setC() : ModelFactory {
// blabla...
}
fun build() : Model {
// An error occurs if any of setA,setB,and setC is not called.
}
}
//example
fun successtest() {
ModelFactory().setA().setB().setC().build() // No error occurs at compile time
}
fun failtest() {
ModelFactory().setA().build() // An error occurs at compile time because setB and setC are not called.
}
解决方法
问题是您将scale_x_discrete
用于连续或数字数据。只需切换到scale_x_continuous
,x轴和标签就会显示:
lb <- c("8AM","2PM","8PM","2AM","8AM")
L <- 1440
xat <- c(1,L/4,L/2,3*L/4,L)
library(ggplot2)
library(ggforce)
ggplot(Baseline.df_long,aes(x = Minute,y = value,color = key,group = key)) +
facet_wrap_paginate(~ key,page = 1,scales = "free_x") +
geom_bar(stat = "identity") +
scale_y_continuous(breaks=seq(0,2500,500),limits =c(0,2500)) +
scale_x_continuous(name="Time",breaks = xat,labels = lb) +
labs(title ="Piglet Activity",y = "Activity") +
theme_bw(base_size = 14) +
theme(legend.position = "none")