更改图中的“y”标签

时间:2014-10-15 12:00:22

标签: r plot label labels

我有一个定义的绘图函数:

> graphic <- function(x,K=5) 
{ 
  p <- length(x) # items 
  n <- nrow(x) # observations 
  r <- 6 # values
  myf <- function(y) # extract frequencies, no missings
  { 
    y <- y[!is.na(y)] 
    y <- as.factor(y) 
    aux <- summary(y) 
    res <- rep(0, r) 
    res[1:r %in% names(aux)] <- aux 
    100 * res / sum(res) 
  } 
> 
> 
> freqs <- apply(x, 2, FUN = myf) # aplica la funció myf per columnes   
> df2 <- expand.grid(vals = 1:r, item = 1:p) # crea totes les
> combinacions possibles    df2$freq <- as.numeric(freqs) # afegeix les
> freqüències    # gràfic    a <- paste("Items 1 to",ncol(x))    titol
> <- paste("Frequency plot for", as.character(substitute(x))) 
>      plot(df2$item,df2$vals,type="n",xlim=c(1,p),ylim=c(1,r),xaxt = "n", 
>        xlab=bquote(.(a)), ylab="Item values (1 to 6)", ann=FALSE) 
>   
>   
> 
>   axis(1, at = 1:length(x) ,labels=names(x), las=2, cex.axis=0.8)   
> title(bquote(.(titol)), 
>         sub = "For each item, square sizes are proportional to frequences ", 
>         cex.main = 1,font.main= 2, col.main= "black", 
>         cex.sub = 1, font.sub = 1, col.sub = "black") 
>      points(df2$item,df2$vals,pch=22,col="black", bg="gray", cex=(df2$freq/n)*K)  }

我无法上传图片,因为我的声誉不允许,但是:https://www.dropbox.com/s/g1h77z3ob0zf2jj/Rplot.png?dl=0 (当我获得汽车声誉时,我将上传文件) 我希望那六个取另一个值,例如&#34; TT&#34;,但我无法找到如何做到这一点。任何的想法?

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

您可以在页面底部的 Axes 部分查看this external link

你需要:

1)使用功能plot(x, y, yaxt="n")中的 yaxt =“n” 选项删除y轴。

2)添加axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...)的自定义y轴,并根据需要精确选择labels = c("1", "2", "3",..., "TT")

以下代码适合您:

## your x-axis is already deleted, so you do the same for y-axis, adding yaxt="n"
plot(df2$item,df2$vals,type="n",xlim=c(1,p),ylim=c(1,r),xaxt = "n", yaxt="n", 
        xlab=bquote(.(a)), ylab="Item values (1 to 6)", ann=FALSE)
## x-axis already created 
axis(1, at = 1:length(x) ,labels=names(x), las=2, cex.axis=0.8) 
## y-axis creation 
axis(2, at = x, labels=c("1","2","3","4","5","TT"), las=2)