绘制因子时改变y轴侧

时间:2014-06-26 07:49:38

标签: r graphics plot axis-labels

我有以下情节

k.factor<-factor(sample(1:5, 100, replace = T))
s.factor<-factor(sample(c("A", "B", "C"), 100, replace = T))
plot(k.factor, s.factor)

我想删除左侧y轴(A,B,C)并将此信息绘制为图例。我怎么能抑制这种yaxis?我还希望概率轴显示为左y轴(而不是现在的右y轴)?我该怎么翻这个?

请仅以R答案为基础

2 个答案:

答案 0 :(得分:1)

如果您不介意使用ggplot2,可以这样做:

library(ggplot2)

ggplot(data.frame(k.factor, s.factor), aes(x = k.factor, fill = s.factor)) + 
   geom_bar(position="fill")

答案 1 :(得分:0)

您可以在绘图语句中设置axes=FALSE,然后使用?axis手动添加轴:

plot(k.factor, s.factor, axes=FALSE)
axis(1)
axis(2)

编辑:事实上,您必须自定义axis语句。

# create offset and at variables
off <- 0.02
xat <- c(0, cumsum(prop.table(table(k.factor)) + off))
# plot 
plot(k.factor, s.factor)
plot(k.factor, s.factor, axes=FALSE, off=100*off)
axis(1, at = (xat[-length(xat)] + xat[-1] - off)/2, 
     labels = 1:5, tick = FALSE)
axis(2)

您还可以查看?spineplot以获取有关参数off的更多信息。

相关问题