7

x 軸のラベルにさまざまなタンパク質の名前が含まれている ggplot でプロットを作成していますが、これらの名前の一部が長すぎてラベルが大きくなりすぎてプロットが見にくくなるため、問題が発生しています。

より大きなグラフを「印刷」する代わりに、x 軸ラベルの文字数を減らす方法はありますか?

私の問題を示す例を次に示します。

library(ggplot2)
dat <- mtcars
# Make the x-axis labels very long for this example
dat$car <- paste0(rownames(mtcars),rownames(mtcars),rownames(mtcars),rownames(mtcars))

ggplot(dat, aes (x=car,y=hp)) +
    geom_bar(stat ="identity", fill="#009E73",colour="black") +
    theme_bw() +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

ここに画像の説明を入力

ラベルを次のように変換したいと思います。

Thisisaveryveryveryloooooongprotein

これに

Thisisavery[...]   

私のプロットが一貫して見えるように

4

2 に答える 2

7

abbreviate文字列からスペースと小文字の母音を削除することで機能するため、奇妙な省略形になる可能性があります。多くの場合、代わりにラベルを切り詰めたほうがよいでしょう。

label=関数の引数に任意の文字列切り捨て関数を渡すことでこれを行うことができますscale_*: いくつかの良いものはstringr::str_trunc、ベース Rstrtrim

mtcars$name <- rownames(mtcars)

ggplot(mtcars, aes(name, mpg)) +
    geom_col() +
    scale_x_discrete(label = function(x) stringr::str_trunc(x, 12)) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

ここに画像の説明を入力

于 2019-10-04T22:28:50.777 に答える