【问题标题】:How to change the plot area in R?如何更改R中的绘图区域?
【发布时间】:2016-02-13 02:35:48
【问题描述】:

我已经搜索了很多关于这个主题的内容。我发现了很多关于它的主题。

但这都不适合我的问题,因为他们中的大多数人都想更改窗口大小或设备大小。另外,如果您在 R 中使用 pdf() 函数。

我正在使用 RSweave 构建一个 pdf 文件。我尝试使用“xlim”或“space”属性。

它们都改变绘图区域的宽度或空间,只是改变条形的宽度。

我也尝试使用 par(mfrow) 但这对我也没有帮助。

我不想更改绘图区域周围的边距。我想让绘图区更宽,因为条的标题消失了,因为没有空间。

这是绘图的代码(记住它在 RSweave 文档中):

<<echo=FALSE, fig=TRUE>>== 
plot(data$SD03, names.arg =c("0-15","15-19","20-24","25-29","30-34",
"35-39","40-44","45- 49","50-54","55-59","60-64","65 Jahre oder Älter"))
# xlim=c(0, 70), space=5
@

结果截图:

希望你能帮助我,谢谢你的回答!

【问题讨论】:

    标签: r plot


    【解决方案1】:

    对基本 R 图形不太熟悉,我提供了一个ggplot2 解决方案(基于从您的屏幕截图中猜测的值):

    library(ggplot2)
    
    value <- c(0, 1, 20, 3, 3, 0, 0, 5, 2, 5, 2, 7)
    names.arg =c("0-15","15-19","20-24","25-29","30-34",
                 "35-39","40-44","45- 49","50-54","55-59","60-64","65 Jahre oder Älter")
    df <- data.frame(names.arg = names.arg, value = value)
    
    ggplot(df, aes(x = names.arg, y = value)) + 
      geom_bar(stat = "identity", fill = "cadetblue", width = 0.9) +
      theme(axis.text.x = element_text(angle = 30)) +
      theme(axis.ticks.x = element_blank()) +
      labs(x = NULL, y = NULL) +
      scale_x_discrete(expand = c(0, 0)) +
      scale_y_continuous(expand = c(0,0))
    

    【讨论】:

    • 谢谢!!!有效。我没有使用值向量,而是使用了我的数据,例如 thiis:ggplot(df1, aes(x = names.arg, y = as.numeric(table(data$SD03)))).... 为我工作! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2012-07-10
    • 2012-03-03
    相关资源
    最近更新 更多