こんにちは、このシンプルなコード(そして今朝からのすべての私のスクリプト)は、ggplot2でタイトルが中心から外れてしまうようになりました。
Ubuntu version: 16.04
R studio version: Version 0.99.896
R version: 3.3.2
GGPLOT2 version: 2.2.0
この問題を解決するために、今朝、上記を新たにインストールしました。....
dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23)
)
# Add title, narrower bars, fill color, and change axis labels
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") +
guides(fill=FALSE) +
xlab("Time of day") + ylab("Total bill") +
ggtitle("Average bill for 2 people")
ggplot 2.2.0のリリースニュースより。["Main plot title is now left-aligned to better work with a subtitle"](https://github.com/tidyverse/ggplot2/releases/tag/v2.2.0).また、
?themeの
plot.title` 引数も参照してください: "left-aligned by default".
J_Fさんが指摘されたように、タイトルを中央に配置するために、theme(plot.title = element_text(hjust = 0.5))
を追加することもできます。
ggplot() +
ggtitle("Default in 2.2.0 is left-aligned")
ggplot() +
ggtitle("Use theme(plot.title = element_text(hjust = 0.5)) to center") +
theme(plot.title = element_text(hjust = 0.5))
Henrikによる回答](https://stackoverflow.com/a/41487043/4303162)で述べられているように、タイトルはggplot 2.2.0からデフォルトで左揃えになります。タイトルは、プロットにこれを追加することで中央揃えにすることができます。
theme(plot.title = element_text(hjust = 0.5))
しかし、多くのプロットを作成する場合、この行をどこにでも追加するのは面倒かもしれません。ggplotのデフォルトの動作を次のように変更することもできます。
theme_update(plot.title = element_text(hjust = 0.5))
この行を実行すると、その後に作成されるすべてのプロットは、テーマ設定 plot.title = element_text(hjust = 0.5)
をデフォルトとして使用します。
theme_update(plot.title = element_text(hjust = 0.5))
ggplot() + ggtitle("Default is now set to centered")
元のggplot2のデフォルト設定に戻すには、Rセッションを再起動するか、次のようにしてデフォルトのテーマを選択します。
theme_set(theme_gray())