簡體   English   中英

r線圖y軸標簽

[英]r line plot y axis labels

我正在嘗試繪制線圖,這是用於生成此圖的代碼和數據集。

ggplot(testdf , aes(measurement, Month)) +
geom_line(aes(group = Month)) +
geom_point(aes(color = Year))

testdf <- structure(list(Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12), Year = c(2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014), measurement = c(0.615151515, 
0.59116161625, 0.609090909, 0.613131313, 0.621212121, 0.63964646425, 
0.619191919, 0.634343434, 0.645454545, 0.676767677, 0.6368686865, 
0.6459595955, 0.656565657, 0.623232323, 0.638383838, 0.6358585855, 
0.651515152, 0.6772727275, 0.675757576, 0.666666667, 0.6964646465, 
0.704040404, 0.6843434345, 0.673737374, 0.6792929295, 0.63813131275, 
0.6641414145, 0.65782828325, 0.6712121215, 0.709090909, 0.7136363635, 
0.686868687, 0.708080808, 0.724242424, 0.7015151515, 0.7237373735
)), .Names = c("Month", "Year", "measurement"), row.names = c(NA, 
-36L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "Month", drop = TRUE, indices = list(
    c(0L, 12L, 24L), c(1L, 13L, 25L), c(2L, 14L, 26L), c(3L, 
    15L, 27L), c(4L, 16L, 28L), c(5L, 17L, 29L), c(6L, 18L, 30L
    ), c(7L, 19L, 31L), c(8L, 20L, 32L), c(9L, 21L, 33L), c(10L, 
    22L, 34L), c(11L, 23L, 35L)), group_sizes = c(3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), biggest_group_size = 3L, labels = structure(list(
    Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)), row.names = c(NA, 
-12L), class = "data.frame", vars = "Month", drop = TRUE, .Names = "Month"))

在此處輸入圖片說明

問題是y軸標簽。 如何更改y軸標簽,以使其顯示標簽(例如1月1日,2月2日,3月3日),而不是2.5、5、7.5等。任何有關如何更改y軸標簽的建議都將受到贊賞。

使用ggplot2lubridate ,您可以執行以下操作:

library(ggplot2)

testdf$Month <- lubridate::month(testdf$Month, label = TRUE)

ggplot(testdf, aes(measurement, Month)) +
  geom_line(aes(group = Month)) +
  geom_point(aes(color = Year))

在此處輸入圖片說明

而且,如果您不想縮寫月份,則可以使用abbr = FALSE修改lubridate行:

lubridate::month(testdf$Month, label = TRUE, abbr = FALSE)

第一行基於此處的答案: https : //stackoverflow.com/a/39423556/5333994

testdf <- within(testdf, Date <- sprintf("%d-%02d-01", Year, Month))

ggplot(testdf , aes(measurement, as.Date(Date, "%Y-%m-%d"))) +
  geom_line(aes(group = Month)) +
  geom_point(aes(color = Year)) +
  scale_y_date()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM