R 如何解决视口错误的非有限位置和/或大小问题?
以下是一个简化且可测试的示例:R 如何解决视口错误的非有限位置和/或大小问题?,r,ggplot2,R,Ggplot2,以下是一个简化且可测试的示例: dataset <- data.frame( emp_month = c("January","March","April","May","December"), salary = c(623.3,515.2,611.0,729.0,843.25)) library(ggplot2) ggplot(dataset)+ geom_boxplot(aes(x = sort(factor(emp_month)), y = salary))+ g
dataset <- data.frame(
emp_month = c("January","March","April","May","December"),
salary = c(623.3,515.2,611.0,729.0,843.25))
library(ggplot2)
ggplot(dataset)+
geom_boxplot(aes(x = sort(factor(emp_month)), y = salary))+
geom_point(aes( x = sort(factor(emp_month)), y=salary))+
facet_grid(. ~ sort(factor(emp_month)),space = "free", scales="free",margins = T)
并将其作为输出:
为了按时间顺序排列月份,我使用了sort
和fact
r:
library(ggplot2)
MesDeConclusao = factor(MesDeConclusao, levels = month.name)
MesDeConclusao = sort(MesDeConclusao)
ggplot(dataset)+
geom_boxplot(aes(x = sort(factor(MesDeConclusao, levels = month.name)), y = Horas.Totais.PE))+
geom_point(aes( x = sort(factor(MesDeConclusao, levels = month.name)), y=Horas.Totais.PE))+
facet_grid(. ~ sort(factor(MesDeConclusao, levels = month.name)),space = "free", scales="free")
结果是:
但是,如果我将margins=T
添加到facet_网格(.~sort(factor(MesDeConclusao,levels=month.name)),space=“free”,scales=“free”,margins=T)
,我会收到以下错误消息:
grid.Call.graphics中出错(C_setviewport,vp,TRUE):
视口的非有限位置和/或大小
调用:FUN->push.vp.viewport->grid.Call.graphics
停止执行
我不明白为什么在图中对因子级别进行排序会有好处;最好在打印数据之前进行处理。以下内容对我来说似乎很好:
# Just to ensure levels are in correct order
dataset$emp_month <- factor(
dataset$emp_month,
levels = c("January", "March", "April", "May", "December")
)
ggplot(dataset) +
geom_boxplot(aes(x = emp_month, y = salary)) +
geom_point(aes(x = emp_month, y = salary)) +
facet_grid(. ~ emp_month ,space = "free", scales = "free", margins = T)
#只是为了确保级别顺序正确
数据集$emp_month起初,我在绘图外对因子级别进行排序,但在附加数据后,我在“emp_noth”上进行排序,因此它不适用于ggplot(data=dataset,…),这就是我在绘图内进行排序的原因。非常感谢。在ggplot()
调用中,我没有进行任何排序就收到了相同的错误。该问题是一个仅具有NA
的刻面因子级别,这导致了space=“free”
的问题。一旦我删除了NA行,它就起作用了。
# Just to ensure levels are in correct order
dataset$emp_month <- factor(
dataset$emp_month,
levels = c("January", "March", "April", "May", "December")
)
ggplot(dataset) +
geom_boxplot(aes(x = emp_month, y = salary)) +
geom_point(aes(x = emp_month, y = salary)) +
facet_grid(. ~ emp_month ,space = "free", scales = "free", margins = T)