【问题标题】:R: scaling , legends and the axis with barplotsR:缩放,图例和带有条形图的轴
【发布时间】:2014-05-07 01:08:43
【问题描述】:

我的图例、缩放文本和命名单个轴时遇到问题

代码:

youtube_1 <- c(1)
youtube_2 <- c(2)
youtube <- rbind( youtube_1 , youtube_2)

facebook_1 <- c(6)
facebook_2 <- c(3)
facebook <- rbind( facebook_1 , facebook_2)

twitter_1 <- c(3)
twitter_2 <- c(1)

twitter <- rbind(twitter_1 , twitter_2)


groups <- rbind(youtube , space , facebook , space , twitter , space)


barplot(
  groups , 
  main="Rating" ,
  ylab="Number of votes over time (people under 50 vs, people over 50)" ,
  xlab="Year 1 , Year 2 , Year 3 " ,
  col=c("yellow" , "green" ) ,
  beside=TRUE ,
  ylim= range(0 , 10) ,
 legend("topright" , legend=c("Young" , "Old") , col=c("yellow" , "green")) ,
  )                

当我让 R 做这件事时,它给了我:

  1. 我的图例背后隐藏着一张图 :(
  2. 与图表相比,y 轴的比例太大
  3. 未标记单个条形
  4. 我也遇到了这个丑陋的错误:

    宽度/2 错误:二元运算符的非数字参数 另外:警告信息: 在 mean.default(width) 中:参数不是数字或逻辑:返回 NA

我想要:

  1. 图例要远离图形“区域”
  2. 缩放轴的文本
  3. 标记每个单独的条
  4. 在 y 轴上标记一个单独的点(例如 5),并在理想情况下通过该点画一条线

我也是 R 的新手,所以请慢慢来,对于傻瓜请:)

【问题讨论】:

    标签: r plot


    【解决方案1】:

    我很难确切地看到您的问题是什么,因为“空间”不是一个定义明确的对象。

    宽度/2 错误:二进制运算符的非数字参数另外:警告消息:在 mean.default(width) 中:参数不是数字或逻辑:返回 NA”是源于不正确地指定图例。您要么需要在 barplot() 中使用 args.legend

    args.legend = list(x = "topleft", col=c("yellow","green"), legend=c("Young","Old"))
    

    或者另一种解决方案是在事后指定图例:

    barplot(
     groups , 
      main="Rating" ,
      ylab="Number of votes over time (people under 50 vs, people over 50)" ,
      xlab="Year 1 , Year 2 , Year 3 " ,
      col=c("yellow" , "green" ) ,
      beside=TRUE ,
      ylim= range(0 , 10) ,
    )
    
    legend("topright" , legend=c("Young" , "Old") , col=c("yellow" , "green")) 
    

    您可以使用 barchart() 中的名称为每个条形指定标签,并且可以使用 cex.names 指定名称的大小:

    barplot(
      groups , 
      main="Rating" ,
      ylab="Number of votes over time (people under 50 vs, people over 50)" ,
      names=row.names(groups),
      cex.names=0.75
      col=c("yellow" , "green" ) ,
      beside=TRUE ,
      ylim= range(0 , 10) ,
    )
    

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2021-02-26
      相关资源
      最近更新 更多