2009-08-25 73 views
420

私はx軸がラベルが長い要素であるプロットを持っています。おそらく理想的な視覚化ではありませんが、今はこれらのラベルを垂直になるように回転させたいと考えています。私はこのコードを以下のコードで理解しましたが、わかるように、ラベルは完全には見えません。ggplot2の軸ラベルの回転と間隔

data(diamonds) 
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut)) 
q <- qplot(cut,carat,data=diamonds,geom="boxplot") 
q + opts(axis.text.x=theme_text(angle=-90)) 

enter image description here

答えて

678

変更デフォルトで

q + theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

への最後の行、軸が回転した場合でも、テキストの中央に整列しています。あなたは+/- 90度回転するときは、通常ではなくエッジに整列されたい:

alt text

上記画像はthis blog postからです。

+80

があろう:それはしない限り 'Q +テーマ(axis.text.x = ELEMENT_TEXT(角度= -90、hjust = 0))' ' – rnorberg

+0

element_text'をもはや存在ggplot2パッケージではありません... – naught101

+38

ここで説明するようにhjustが動作しない人には、 'theme(axis.text.x = element_text(angle = 90、vjust = 0.5))'を試してください。 ggplot2 0.9.3.1現在、これが解決策であるようです。 – lilster

57

目盛ラベル上のテキストが完全に見えるように、y軸のラベルと同じ方向で読み取ら、I、代替ソリューションを提供したい

q + theme(axis.text.x=element_text(angle=90, hjust=1)) 
+1

: '' opts'' ---> '' theme''と '' theme_text'' ---> '' element_text'' – PatrickT

15

の最後の行を変更し、Aとキャンバスローテーション機能を導入して以来、私が提案しようとしているものに似た堅牢なソリューションが最新のバージョンggternで必要でした。

element_textオブジェクト、角度(度)、位置(x、y、上または右のいずれか)の情報を返す関数を構築することによって、三角法を使用して相対位置を決定する必要があります。

#Load Required Libraries 
library(ggplot2) 
library(gridExtra) 

#Build Function to Return Element Text Object 
rotatedAxisElementText = function(angle,position='x'){ 
    angle  = angle[1]; 
    position = position[1] 
    positions = list(x=0,y=90,top=180,right=270) 
    if(!position %in% names(positions)) 
    stop(sprintf("'position' must be one of [%s]",paste(names(positions),collapse=", ")),call.=FALSE) 
    if(!is.numeric(angle)) 
    stop("'angle' must be numeric",call.=FALSE) 
    rads = (angle - positions[[ position ]])*pi/180 
    hjust = 0.5*(1 - sin(rads)) 
    vjust = 0.5*(1 + cos(rads)) 
    element_text(angle=angle,vjust=vjust,hjust=hjust) 
} 

率直に言って、私の意見では、私は角度を指定するときに「自動」オプションは、hjustvjust引数にggplot2で利用できるようにすべきだと思い、とにかく、どのように上記の作品を発揮することができます。以下を生成

#Demonstrate Usage for a Variety of Rotations 
df = data.frame(x=0.5,y=0.5) 
plots = lapply(seq(0,90,length.out=4),function(a){ 
    ggplot(df,aes(x,y)) + 
    geom_point() + 
    theme(axis.text.x = rotatedAxisElementText(a,'x'), 
      axis.text.y = rotatedAxisElementText(a,'y')) + 
    labs(title = sprintf("Rotated %s",a)) 
}) 
grid.arrange(grobs=plots) 

Example

+1

私は同じものを取得しません結果、私のために、軸テキストはあなたの自動メソッドを使ってうまく調整されません。しかし、 'rads =(-angle-positions [[position]])* pi/180'を使うとより良い配置ができました。角度の前に追加のマイナス記号があることに注意してください。とにかくコードをありがとう:) –

16

使用+ coord_flip()

「R for Data Science」では、WickhamとGrolemundがこの正確な問題を話します。章3.8、位置調整では、それらは書き込み:

coord_flip()は、x軸とy軸を切り替えます。これは、水平ボックスプロットが必要な場合などに便利です(例)。また、長いラベルの場合にも便利です.X軸に重ならないようにフィットさせるのは難しいです。あなたのプロットにこれを適用する

、私達はちょうどggplotに+ coord_flip()を追加します。

data(diamonds) 
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut)) 

qplot(cut,carat,data = diamonds, geom = "boxplot") + 
    coord_flip() 

enter image description here

そして今、超ロングタイトルが水平に広がっており、読み取りが非常に簡単!

ggplot2コマンドの最新版で
関連する問題