我有一個使用ggplot2的繪圖,其中我想沿x軸繪製很多刻度線,但只有一些刻度線會有與它們相關的刻度標籤。不過,我希望那些有標籤的標記比那些標記更長。ggplot2:對於沒有標籤的刻度線有更短的刻度標記
9
A
回答
1
theme(axis.ticks.length)
http://docs.ggplot2.org/0.9.2.1/theme.html應該是有用的。
編輯對不起,只是仔細閱讀你的問題。標籤的是標準的major.grid ticks嗎?或以某種自定義的方式標記?
8
在基R,可以與初始plot
呼叫(具有xaxt='n'
)抑制x軸,然後使用tcl
控制蜱長度(參見?par
)具有兩個axis(1, ...)
呼叫。
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)
3
這是基礎的圖形更容易,但這裏是使用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)
+1
這對我有用。除了我將標記重置的行改爲:'marks $ y [seq(1,length(marks $ y),2)] [ - seq(1,length(marks $ y)/ 2,n_minor + 1)] < - unit(1,'npc') - unit(3,'pt')' 使用這種方法,您還應該設置'axis.ticks.length = unit(6,「pt」)'在主題中。 – aaiezza
相關問題
- 1. Dygraphs:x軸上沒有刻度和刻度標籤
- 2. ggplot2中的X軸刻度標記
- 3. 旋轉後沒有刻度標籤
- 4. R,軸的刻度線和刻度標記之間變化距離標籤
- 5. R - ggplot2 - 設置刻度標記間隔
- 6. ALTER刻度標記
- 7. 軸刻度標記
- 8. ggplot x軸刻度標記標籤
- 9. 如何更改matplotlib中的日誌刻度刻度標籤
- 10. Java - 繪製標尺(在90度角刻有刻度線)
- 11. Python - 刪除軸刻度標籤,保留刻度和軸標籤
- 12. D3中帶標籤的線性刻度
- 13. matplotlib中的刻度標記
- 14. 下標和在x軸刻度標籤在GGPLOT2寬度限制
- 15. 在ggplot2中按年分組的刻度線標記分組
- 16. 線性刻度對比對數刻度
- 17. Chart.js數據/標籤刻度
- 18. 格式軸刻度標籤
- 19. ggplot2中的自定義座標軸刻度標籤
- 20. Matplotlib刻度線和刻度線標籤位置與軸線分開固定
- 21. 圓環圖與刻度標記線(D3)
- 22. Spreadsheetgear刻度線標記位置
- 23. 刻度線標記顯示順序
- 24. ř圖軸刻度標記
- 25. 在Tkinter中標記刻度
- 26. Tilemill標註刻度線
- 27. ggplot2:模糊刻面標籤
- 28. R rgl軸刻度和刻度標記之間的距離
- 29. 如何使用ggplot2更改軸標籤和刻度標籤之間的間距?
- 30. 強制點陣對所有座標軸刻度使用標籤
他們major.grid蜱,使用'主題(axis.ticks.length)'會定義所有的長度不僅是那些有標籤的人。 – baisong