2017-11-28 5 views
1

私は出版物用のグラフを生成していますが、ggplot自体で図のパネルにラベルを付けることができます出版社などへ)、最終的な文書にきちんと合っているようにしてください。私はタイトルに手紙( "A")を追加することでこれをやろうとしていましたが、タイトルを中心にしたいと思います。左上の手紙を欲しいです。今プロットラベル(文字 "A"など)をggplot2プロットに追加

# base graph:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+ 
    geom_jitter(size = 6.5)+ 
    ggtitle("A \n \n The Actual Long, Normal Title of Titliness")+ 
    theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30), 
     axis.ticks = element_blank(), 
     legend.text = element_text(size = 25), 
     axis.title = element_text(size = 25, face = "bold"), 
     axis.text = element_text(size = 25, vjust = 0.05), 
     legend.position = "bottom") 

oh noes! both of them are centered over the graph

私は手の間隔各タイトルでわずか「それ偽物」と喜んでいる場合、私は一種のそれが仕事を得ることができますが、これは時間のようです集中的かつ原油的である。

# sloppy solution 
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+ 
    geom_jitter(size = 6.5)+ 
    ggtitle("A \n \n    The Actual Long, Normal Title of Titliness")+ 
    theme(plot.title = element_text(hjust = 0,face = "bold", size = 30), 
     axis.ticks = element_blank(), 
     legend.text = element_text(size = 25), 
     axis.title = element_text(size = 25, face = "bold"), 
     axis.text = element_text(size = 25, vjust = 0.05), 
     legend.position = "bottom") 

better spacing but sloppy code

、独自のhjust値ごとに個別のタイトルの各「行」をコールする方法はありますか?

他の創造的なソリューションはありますか?

また、私はmtext(Splitting axis labels with expressions)の可能性を見ましたが、ggplot2を使って実装する方法を理解できませんでした(対ベースプロット関数..互換性がないようです)。 この投稿は面白い(Multi-line ggplot Title With Different Font Size, Face, etc)のすべての種類だったが、私はまだRの新人であり、変更するにはこの巧妙なものを編集する方法を理解できなかったインデント

ありがとうございます!

+0

[cowplot](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html)のようなサウンドが必要な場合があります。 – neilfws

+0

私はcowplotを使って答えを加えました。 –

答えて

3

私は特にこの目的のために書いたcowplotパッケージを使用して、私はそれを行うだろうかです。あなたはクリーンなテーマをボーナスとして得ることに注意してください。

library(cowplot) 

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species)) + 
    geom_jitter(size = 3.5) + 
    ggtitle("The Actual Long, Normal Title of Titliness") 

# add one label to one plot 
ggdraw(p) + draw_plot_label("A") 

enter image description here

# place multiple plots into a grid, with labels 
plot_grid(p, p, p, p, labels = "AUTO") 

enter image description here

save_plotは、あなたがにスケーリング右の相対を得るのを助けるデフォルト設定とパラメータを持っているので、あなたは、その後、プロットの代わりggsaveを保存するためにsave_plot機能を使用したいですテーマ、特にグリッド内のプロット用。

+0

私はすでにcowplotを使っていましたが、最大の可能性はありません(明らかに) - ありがとう!これは完璧です!! – CatsCauseTypos

1

パッケージgridgrid.textを使用して、プロットにテキストを追加できます。まず、タイトルの上にスペースを追加してください。plot.margin

library(grid) 
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+ 
geom_jitter(size = 6.5)+ 
ggtitle("The Actual Long, Normal Title of Titliness")+ 
theme(plot.title = element_text(hjust = 0,face = "bold", size = 30), 
     axis.ticks = element_blank(), 
     legend.text = element_text(size = 25), 
     axis.title = element_text(size = 25, face = "bold"), 
     axis.text = element_text(size = 25, vjust = 0.05), 
     legend.position = "bottom", 
     plot.margin = unit(c(4, 3, 1, 1), "lines")) 
p 
grid.text('A', x = unit(0.1, 'npc'), y = unit(.90, 'npc'), gp = gpar(fontsize=28)) 

enter image description here

1

最も簡単な方法は、タイトルとサブタイトルを使用することはおそらくあり、

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+ 
    geom_jitter(size = 6.5) + 
    labs(title = "A", subtitle = "The Actual Long, Normal Title of Titliness") + 
    theme(plot.subtitle = element_text(hjust = 0.5)) 
+0

これはシンプルで、私はhjustを個別に、要望通りに呼び出すことができます! – CatsCauseTypos

1

あなたはGrobsを操作することによって、これを行うことができます。 gtablegridggplot2の上に使用します。ここで

library("ggplot2") 
library("gtable") 
library("grid") 

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species, shape = Species))+ 
    geom_jitter(size = 6.5)+ 
    ggtitle("The Actual Long, Normal Title of Titliness")+ 
    theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 30), 
     axis.ticks = element_blank(), 
     legend.text = element_text(size = 25), 
     axis.title = element_text(size = 25, face = "bold"), 
     axis.text = element_text(size = 25, vjust = 0.05), 
     legend.position = "bottom", 
     plot.margin = unit(c(4, 1, 1, 4), "lines")) # We need bigger top/left margins to show the letter 

gt <- ggplot_gtable(ggplot_build(p)) # Building a gtable from the ggplot object 
# Adding the textGrob for the panel letter 
panel_letter = textGrob("A", x = 0.5, y = .9, 
         just = c("left", "top"), 
         gp = gpar(fontsize = 40, col = "black", face="bold")) 
gt <- gtable_add_grob(gt, panel_letter, t=1, l=1, r=1) # Append the Grob to the table 

p <- grid.draw(gt) # Display the plot 
ggsave("plot.png",gt, width=10, height=10) # If you want to save it. 

My attempt

関連する問題