の最後の行を変更し、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)
}
率直に言って、私の意見では、私は角度を指定するときに「自動」オプションは、hjust
とvjust
引数に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)
:
があろう:それはしない限り 'Q +テーマ(axis.text.x = ELEMENT_TEXT(角度= -90、hjust = 0))' ' – rnorberg
element_text'をもはや存在ggplot2パッケージではありません... – naught101
ここで説明するようにhjustが動作しない人には、 'theme(axis.text.x = element_text(angle = 90、vjust = 0.5))'を試してください。 ggplot2 0.9.3.1現在、これが解決策であるようです。 – lilster