7

ggplot2 を使用して散布図を生成しています。タイトルを変数にしましたが、フォントサイズを変更するにはどうすればよいですか? コードは次のとおりです。

library("ggplot2")
plotfunc <- function(x){
    x +
    geom_point() +
    geom_smooth(se = FALSE, method = "lm",  color = "blue", size = 1) +
    opts(title = plottitle,
           axis.title.x = theme_text(size = 8, colour = 'black'),
         axis.title.y = theme_text(size = 8, colour = 'black', angle = 90))
}

plottitle <- "This is Title"
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
plotfunc(p)

私は試した

opts(title = plottitle (size = 10),...

しかし、エラーがありました:

Error in opts(title = plottitle(size = 10),
axis.title.x = theme_text(size = 8,  : could not find function "plottitle"

自分の求めていない機能として認識されました。私は何をすべきか?ありがとう。

4

3 に答える 3

8

opts()がまだ機能する場合は、古いバージョンの ggplot2 を使用しています。新しいコマンドはtheme()です。いずれにせよ、実際のタイトル ラベルをoptsまたはthemeに入れたくありません-- labs()を使用してください

plotfunc <- function(x) {
  x +
    geom_point() +
    geom_smooth(se = FALSE, method = "lm",  color = "blue", size = 1) +
    theme_bw() +
    theme(axis.title.x = element_text(size = 8, colour = 'black'),
          axis.title.y = element_text(size = 8, colour = 'black', angle = 90)) +
    labs(title='this', x='that', y='the other')
}

## plottitle <- "This is Title"
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
plotfunc(p)

ここに画像の説明を入力

于 2012-11-27T18:17:17.297 に答える
6

ばかばかしいほど遅い回答ですが、既存の回答が実際の質問に対処しているとは思いませんでした:

タイトルを変数にしましたが、フォントサイズを変更するにはどうすればよいですか?

これは私にとってはうまくいき、更新されたggplot構文にあります(theme()vs. opts()):

library(ggplot2)
plotfunc <- function(x){
    x +
    geom_point() +
    geom_smooth(se = FALSE, method = "lm",  color = "blue", size = 1) +
    labs(title = plottitle) +
    ### pay attention to the ordering of theme_bw() vs. theme()
    theme_bw() +
    theme(plot.title = element_text(size = 20),
          axis.title.x = element_text(size = 12),
          axis.title.y = element_text(size = 8))

}

plottitle <- "This is Title"
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
plotfunc(p)

私は以下を取得します:

ggplot テーマ (plot.title)

theme_bw()私のコメントについてのメモ: 上記を実行しtheme_bw()てみてくださいtheme(plot.title, ...)。どのフォント サイズも有効にならないことに注意してください。これは、追加後に渡すと、theme_bw()さまざまなカスタム オプションを上書きするプリセットであるためです。theme()

注意すべきちょっとしたことです。結局のところ、それが私の構文ではなく、設定の順序であることに気付く前に、他のオプションが機能しなかっtheme_bw()た理由を理解しようとして、多くのことを使用して頭を壁にぶつけたためにそれを学びました。コーディングが大好きです:)theme()ggplot

また、何をどのように微調整できるかの参考として、渡すことができるオプションの完全なリストを次に示します。theme()

于 2015-04-22T18:56:08.210 に答える
1

次の非空白文字として「(」を配置するplottitleと、インタープリターはそれが関数でなければならないと判断しました。試してください。

  .... opts( title=plottile, size=10)

これは警告メッセージの長いリストです:

Warning messages:
1: 'opts' is deprecated.
Use 'theme' instead.
See help("Deprecated") 
2: 'theme_text' is deprecated.
Use 'element_text' instead.
See help("Deprecated") 
3: 'theme_text' is deprecated.
Use 'element_text' instead.
See help("Deprecated") 
4: In opts(title = plottitle, axis.title.x = theme_text(size = 8, colour = "black"),  :
  Setting the plot title with opts(title="...") is deprecated. Use labs(title="...") or ggtitle("...") instead.
于 2012-11-27T17:43:44.660 に答える