ggplot2を使用してx軸に多くの目盛りを付けるプロットがありますが、ティックの一部だけに目盛りラベルが関連付けられています。しかし、ラベルが付いているものの目盛りは、そうでないものよりも長くなるようにしたいと思います。ggplot2:ラベルなしのティックマークの目盛りが短い
答えて
theme(axis.ticks.length)
http://docs.ggplot2.org/0.9.2.1/theme.html有用なはずです。
編集申し訳ありません、あなたの質問をもっと慎重に読んでください。ラベルされたものは標準的なmajor.grid ticksですか?またはいくつかのカスタム方法でラベル?基地Rで
、次の2回のaxis(1, ...)
呼び出しで(?par
を参照)(xaxt='n'
有する)初期plot
コールとx軸を抑制し、その後、目盛りの長さを制御するtcl
を使用することができます。
plot(1:10, xaxt='n', ylab='', las=1)
axis(1, at=1:10, tcl=-0.8)
axis(1, at=seq(0, 11, 0.2), labels=NA)
これは、ベースのグラフィックと簡単ですが、ここでggplot2を使用しての試みです。
この方法は、this methodに基づいています。これは、ラベルのシーケンスに空白のラベルを挿入します(その回答のコードを使用)。 ggplotの中にブレークとラベルが配置されたら、私は目盛りの長さを編集するためにプロットの構造を掘り下げます。 (注意して使用してください。これを破るのは難しいことではないでしょう。)
library(ggplot2)
library(grid)
# Data
df = data.frame(x = 1:10, y = 1:10)
# Range of x values
range = 1:10
# Major tick marks
major = 1
# Minor tick marks
minor = 0.2
# Function to insert blank labels
# Borrowed from https://stackoverflow.com/questions/14490071/adding-minor-tick-marks-to-the-x-axis-in-ggplot2-with-no-labels/14490652#14490652
insert_minor <- function(major, n_minor) {
labs <- c(sapply(major, function(x, y) c(x, rep("", y)), y = round(n_minor)))
labs[1:(length(labs) - n_minor)]
}
# Getting the 'breaks' and 'labels' for the ggplot
n_minor = major/minor - 1
breaks = seq(min(range), max(range), minor)
labels = insert_minor(seq(min(range), max(range), major), n_minor)
if(length(breaks) > length(labels)) labels = c(labels, rep("", length(breaks) - length(labels)))
# The plot
p <- ggplot(df, aes(x = x, y = y)) +
geom_point() +
scale_x_continuous(breaks = breaks, labels = labels) +
coord_cartesian(xlim = range) +
theme_bw() +
theme(panel.grid = element_blank(),
axis.text.x = element_text(margin = margin(t = 5, unit = "pt")))
p
# Edit the plot:
# Change the lengths of the major tick marks
g = ggplotGrob(p)
# Get the x axis
xaxis <- g$grobs[[which(g$layout$name == "axis-b")]]
# Get the tick marks and tick mark labels
ticks <- xaxis$children[[2]]
# Get the tick marks
marks = ticks$grobs[[1]]
# Edit the y positions of the end points of the tick marks
# The '6' and the '3' in the code below
# are the lengths in pts of the major and minor tick marks respectively.
marks$y = unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), unit(1, "npc"),
rep(unit.c(unit(1, "npc") - unit(3, "pt"), unit(1, "npc")), n_minor)))
# Put the tick marks back into the plot
ticks$grobs[[1]] = marks
xaxis$children[[2]] = ticks
g$grobs[[which(g$layout$name == "axis-b")]] = xaxis
# Draw the plot
grid.newpage()
grid.draw(g)
これは私のために働いた。マークを$ y [seq(1、length(marks $ y)、2)] [ - seq(1、length(marks $ y)/ 2、n_minor + このようにして、 'axis.ticks.length = unit(6、" pt ")'と 'テーマで。 – aaiezza
- 1. 整数目盛りラベルmatplotlib、起点目盛りラベルなし
- 2. 目盛りがggplot2 [R]
- 3. ラウンド目盛り動的ggplot2
- 4. ggplot2のx軸に小さな目盛りを付ける(ラベルなし)
- 5. フォーマット軸の目盛りラベル
- 6. Rとggplot2:ログにない目盛りラベル付きのscale_x_continuous(trans = "log")
- 7. Q:R ggplot2 - 別々の目盛りの精度が異なる
- 8. matplotlib format_major_ticks軸の目盛りのラベルを取得しない
- 9. x軸目盛りラベルを変更するggplot2が機能しない、軸が消える、scale_x_discrete
- 10. ggplot2ファセット折り返し:最初の行のy軸目盛り
- 11. データ値のD3目盛りラベル
- 12. 第2変数をggplot2ファセットの軸目盛りにラベルを付ける
- 13. ggplot2を使用してRのx軸目盛ラベルを変更する
- 14. Matplotlib:着色軸/目盛りラベル
- 15. Python PyQt4スライダ(目盛りラベル付き)
- 16. プロット大目盛とラベル
- 17. R 3Dプロット軸目盛ラベル
- 18. matplotlib.pyplotの目盛りラベルの異なる回転
- 19. 軸のインラインスタイルのストローク幅が太字の目盛りラベル
- 20. ggplot2の年ごとの目盛りのグループ化
- 21. LiveChartsの軸目盛りラベルのフォーマットを変更します
- 22. Matplotlibがプロットエリア内の目盛りラベルを移動
- 23. ggplot2軸目盛りと同じ太さの線
- 24. d3.jsの軸目盛ラベルのスタイル
- 25. Matplotlibにx目盛りラベルがありません
- 26. 日付を使用してcontourfプロットのx軸目盛りラベル
- 27. jqPlot軸の目盛りが基になるデータと異なるラベル
- 28. Matplotlibの目盛りラベルのフォントサイズを小さくする
- 29. Boxplot 2つのボックスのx軸目盛りラベル
- 30. 目盛り
彼らは 'すべての長さを定義します'テーマ(axis.ticks.length)を使用して、major.gridティックですマークが付いたものだけでなく、 – baisong