如何根据时间序列折线图上的最后一个值着色?

Posted

技术标签:

【中文标题】如何根据时间序列折线图上的最后一个值着色?【英文标题】:How to color based on the last value on a time series line graph? 【发布时间】:2021-12-16 16:55:38 【问题描述】:

我有以下数据 (mydata):

structure(list(Provinsi = c("ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH"), Persenproposi = c(14.8500365764448, 15.5075939248601,  16.6821994408201, 20.0239808153477, 21.0322580645161, 22.1628838451268)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
                                             

我正在为每个省份(使用 facet_wrap 和 geom_line)绘制 Proporsi(y 轴)和 Date(x 轴)。

mydata_ends <- mydata %>% 
  group_by(Provinsi) %>% 
  top_n(1, Date)

ggplot(data=mydata, aes(x=Date,y=Proporsi, colors (if(Proporsi>=0.4)"RED", else if (Proporsi<=0.2)"GREEN"), else "Yellow") +
  xlab("Tanggal")+
  ylab("Proporsi Keterisian TT/Kasus Aktif")+
  scale_y_continuous(labels = scales::percent_format(accurracy=1))+
  geom_line() +
  geom_text_repel(aes(label = Proporsi), data = mydata_ends, size = 3)+
  facet_wrap(~Provinsi, ncol=5) +
  ggtitle("Proporsi KeterisianTT/Kasus Aktif") +
  theme(plot.title = element_text(family="Trebuchet MS", face="bold", size=20, hjust=0, color="#555555")) +
  theme(axis.text.x = element_text(angle=90))

我一直在尝试使用 ggrepel 和其他着色 if 函数,但它们不起作用。

感谢任何帮助,非常感谢!

structure(list(Provinsi = c("ACEH", "ACEH", "ACEH", "ACEH", "ACEH", 
"ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", 
"ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", 
"ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "ACEH", "BALI", "BALI", 
"BALI", "BALI", "BALI", "BALI", "BALI", "BALI", "BALI", "BALI", 
"BALI", "BALI", "BALI", "BALI", "BALI", "BALI", "BALI", "BALI", 
"BALI", "BALI", "BALI", "BALI", "BALI", "BALI", "BALI", "BALI", 
"BALI", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", 
"BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", 
"BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", 
"BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", "BANTEN", 
"BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", 
"BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", 
"BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", 
"BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", "BENGKULU", 
"BENGKULU", "BENGKULU", "BENGKULU", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", 
"DAERAH ISTIMEWA YOGYAKARTA", "DAERAH ISTIMEWA YOGYAKARTA", "DKI JAKARTA", 
"DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", 
"DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", 
"DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", 
"DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", 
"DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", "DKI JAKARTA", 
"DKI JAKARTA", "GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", 
"GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", 
"GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", 
"GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", 
"GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", "GORONTALO", 
"GORONTALO", "GORONTALO", "GORONTALO", "JAMBI", "JAMBI", "JAMBI", 
"JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", 
"JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", 
"JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", "JAMBI", 
"JAMBI", "JAMBI", "JAMBI", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", 
"JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", 
"JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", 
"JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", 
"JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", 
"JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA BARAT", "JAWA TENGAH", 
"JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", 
"JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", 
"JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", 
"JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", 
"JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", "JAWA TENGAH", 
"JAWA TENGAH", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", 
"JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", 
"JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", 
"JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", 
"JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR", 
"JAWA TIMUR", "JAWA TIMUR", "JAWA TIMUR"), Date = structure(c(1633392000, 
1633478400, 1633564800, 1633651200, 1633737600, 1633824000, 1633910400, 
1633996800, 1634083200, 1634169600, 1634256000, 1634342400, 1634428800, 
1634515200, 1634601600, 1634688000, 1634774400, 1634860800, 1634947200, 
1635033600, 1635120000, 1635206400, 1635292800, 1635379200, 1635465600, 
1635552000, 1635638400, 1633392000, 1633478400, 1633564800, 1633651200, 
1633737600, 1633824000, 1633910400, 1633996800, 1634083200, 1634169600, 
1634256000, 1634342400, 1634428800, 1634515200, 1634601600, 1634688000, 
1634774400, 1634860800, 1634947200, 1635033600, 1635120000, 1635206400, 
1635292800, 1635379200, 1635465600, 1635552000, 1635638400, 1633392000, 
1633478400, 1633564800, 1633651200, 1633737600, 1633824000, 1633910400, 
1633996800, 1634083200, 1634169600, 1634256000, 1634342400, 1634428800, 
1634515200, 1634601600, 1634688000, 1634774400, 1634860800, 1634947200, 
1635033600, 1635120000, 1635206400, 1635292800, 1635379200, 1635465600, 
1635552000, 1635638400, 1633392000, 1633478400, 1633564800, 1633651200, 
1633737600, 1633824000, 1633910400, 1633996800, 1634083200, 1634169600, 
1634256000, 1634342400, 1634428800, 1634515200, 1634601600, 1634688000, 
1634774400, 1634860800, 1634947200, 1635033600, 1635120000, 1635206400, 
1635292800, 1635379200, 1635465600, 1635552000, 1635638400, 1633392000, 
1633478400, 1633564800, 1633651200, 1633737600, 1633824000, 1633910400, 
1633996800, 1634083200, 1634169600, 1634256000, 1634342400, 1634428800, 
1634515200, 1634601600, 1634688000, 1634774400, 1634860800, 1634947200, 
1635033600, 1635120000, 1635206400, 1635292800, 1635379200, 1635465600, 
1635552000, 1635638400, 1633392000, 1633478400, 1633564800, 1633651200, 
1633737600, 1633824000, 1633910400, 1633996800, 1634083200, 1634169600, 
1634256000, 1634342400, 1634428800, 1634515200, 1634601600, 1634688000, 
1634774400, 1634860800, 1634947200, 1635033600, 1635120000, 1635206400, 
1635292800, 1635379200, 1635465600, 1635552000, 1635638400, 1633392000, 
1633478400, 1633564800, 1633651200, 1633737600, 1633824000, 1633910400, 
1633996800, 1634083200, 1634169600, 1634256000, 1634342400, 1634428800, 
1634515200, 1634601600, 1634688000, 1634774400, 1634860800, 1634947200, 
1635033600, 1635120000, 1635206400, 1635292800, 1635379200, 1635465600, 
1635552000, 1635638400, 1633392000, 1633478400, 1633564800, 1633651200, 
1633737600, 1633824000, 1633910400, 1633996800, 1634083200, 1634169600, 
1634256000, 1634342400, 1634428800, 1634515200, 1634601600, 1634688000, 
1634774400, 1634860800, 1634947200, 1635033600, 1635120000, 1635206400, 
1635292800, 1635379200, 1635465600, 1635552000, 1635638400, 1633392000, 
1633478400, 1633564800, 1633651200, 1633737600, 1633824000, 1633910400, 
1633996800, 1634083200, 1634169600, 1634256000, 1634342400, 1634428800, 
1634515200, 1634601600, 1634688000, 1634774400, 1634860800, 1634947200, 
1635033600, 1635120000, 1635206400, 1635292800, 1635379200, 1635465600, 
1635552000, 1635638400, 1633392000, 1633478400, 1633564800, 1633651200, 
1633737600, 1633824000, 1633910400, 1633996800, 1634083200, 1634169600, 
1634256000, 1634342400, 1634428800, 1634515200, 1634601600, 1634688000, 
1634774400, 1634860800, 1634947200, 1635033600, 1635120000, 1635206400, 
1635292800, 1635379200, 1635465600, 1635552000, 1635638400, 1633392000, 
1633478400, 1633564800, 1633651200, 1633737600, 1633824000, 1633910400, 
1633996800, 1634083200, 1634169600, 1634256000, 1634342400, 1634428800, 
1634515200, 1634601600, 1634688000, 1634774400, 1634860800, 1634947200, 
1635033600, 1635120000, 1635206400, 1635292800, 1635379200, 1635465600, 
1635552000, 1635638400), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), Proporsi = c(0.148500365764448, 0.155075939248601, 
0.166821994408201, 0.200239808153477, 0.210322580645161, 0.221628838451268, 
0.245283018867925, 0.250853242320819, 0.222826086956522, 0.30859375, 
0.338297872340426, 0.316901408450704, 0.372037914691943, 0.367875647668394, 
0.451104100946372, 0.546938775510204, 0.533632286995516, 0.516279069767442, 
0.428571428571429, 0.615384615384615, 0.617801047120419, 0.556179775280899, 
0.585798816568047, 0.389937106918239, 0.705479452054795, 0.830769230769231, 
0.888888888888889, 0.483547925608011, 0.448795180722892, 0.464462809917355, 
0.477508650519031, 0.462522851919561, 0.453667953667954, 0.533477321814255, 
0.591346153846154, 0.55050505050505, 0.551724137931034, 0.611111111111111, 
0.612582781456954, 0.675409836065574, 0.726235741444867, 0.82824427480916, 
0.778625954198473, 0.839662447257384, 0.990654205607477, 0.917948717948718, 
0.936507936507937, 1.14838709677419, 1.26760563380282, 1.1203007518797, 
1.33644859813084, 1.52083333333333, 1.6304347826087, 1.46875, 
0.154362416107383, 0.141891891891892, 0.132275132275132, 0.14962962962963, 
0.153030303030303, 0.163779527559055, 0.165605095541401, 0.138041733547352, 
0.135514018691589, 0.135258358662614, 0.133834586466165, 0.138686131386861, 
0.13543599257885, 0.138632162661738, 0.143897996357013, 0.172335600907029, 
0.173027989821883, 0.161458333333333, 0.145780051150895, 0.211538461538462, 
0.226053639846743, 0.285714285714286, 0.225806451612903, 0.221789883268482, 
0.194756554307116, 0.189922480620155, 0.195571955719557, 0.230769230769231, 
0.231578947368421, 0.345238095238095, 0.24, 0.220779220779221, 
0.233766233766234, 0.208333333333333, 0.267605633802817, 0.277777777777778, 
0.3125, 0.216216216216216, 0.197183098591549, 0.208333333333333, 
0.208333333333333, 0.26865671641791, 0.264705882352941, 0.323529411764706, 
0.348484848484849, 0.212121212121212, 0.149253731343284, 0.208955223880597, 
0.258064516129032, 0.1875, 0.15625, 0.171875, 0.171875, 0.171875, 
0.243654822335025, 0.238565022421525, 0.228383458646617, 0.226874391431353, 
0.200205338809035, 0.190782422293676, 0.209919261822376, 0.192401960784314, 
0.191157347204161, 0.207520891364903, 0.199391171993912, 0.242375601926164, 
0.276632302405498, 0.305400372439479, 0.330693069306931, 0.324435318275154, 
0.34341252699784, 0.330357142857143, 0.309352517985612, 0.392592592592593, 
0.355329949238579, 0.380829015544041, 0.380952380952381, 0.330601092896175, 
0.3359375, 0.282776349614396, 0.360824742268041, 0.69305724725944, 
0.681953543776057, 0.64661214953271, 0.68118572292801, 0.422915416916617, 
0.426182237600923, 0.489071038251366, 0.564325177584846, 0.534277198211624, 
0.489705882352941, 0.432394366197183, 0.474926253687316, 0.479734708916728, 
0.522831050228311, 0.51386748844376, 0.526771653543307, 0.529411764705882, 
0.516984258492129, 0.523975588491718, 0.532637075718016, 0.573308270676692, 
0.678532901833873, 0.690744920993228, 0.637139807897545, 0.61522633744856, 
0.590465872156013, 0.574712643678161, 0.0579710144927536, 0.0882352941176471, 
0.19047619047619, 0.142857142857143, 0.0476190476190476, 0.0869565217391304, 
0.130434782608696, 0.12, 0.185185185185185, 0.166666666666667, 
0.142857142857143, 0.032258064516129, 0, 0.0238095238095238, 
0.0666666666666667, 0.0869565217391304, 0.0638297872340425, 0.0377358490566038, 
0.0196078431372549, 0.037037037037037, 0.0166666666666667, 0, 
0, 0.0344827586206897, 0.0338983050847458, 0.0344827586206897, 
0.0689655172413793, 0.156976744186047, 0.126801152737752, 0.115044247787611, 
0.115727002967359, 0.107361963190184, 0.13, 0.127208480565371, 
0.116541353383459, 0.125984251968504, 0.122362869198312, 0.0956521739130435, 
0.103139013452915, 0.140625, 0.144736842105263, 0.227642276422764, 
0.273684210526316, 0.227272727272727, 0.246753246753247, 0.287671232876712, 
0.289473684210526, 0.276923076923077, 0.288461538461538, 0.3, 
0.26, 0.297872340425532, 0.297872340425532, 0.272727272727273, 
0.268315445636958, 0.271113243761996, 0.256658595641647, 0.225711481844946, 
0.227474150664697, 0.229614807403702, 0.239833159541189, 0.249733759318424, 
0.241122565864834, 0.235055136390017, 0.244874048037493, 0.211981566820276, 
0.207656612529002, 0.211714460036608, 0.228842247799594, 0.232904536222072, 
0.256134969325153, 0.276629570747218, 0.274821286735504, 0.268691588785047, 
0.298230834035383, 0.281067556296914, 0.261344537815126, 0.25604670558799, 
0.243498817966903, 0.211482558139535, 0.208860759493671, 0.145617667356798, 
0.137005163511188, 0.138736263736264, 0.131660364386387, 0.133039945836154, 
0.125634947511006, 0.124826147426982, 0.120970537261698, 0.153423499577346, 
0.144299537231805, 0.138256762559038, 0.144809910294746, 0.154576856649396, 
0.169515011547344, 0.166277440448389, 0.16196205460435, 0.169491525423729, 
0.156326331216414, 0.162421912542047, 0.160140562248996, 0.165185572399373, 
0.162790697674419, 0.148958333333333, 0.151090342679128, 0.142406726221755, 
0.131509731720147, 0.122567069963177, 0.641673243883189, 0.624897624897625, 
0.669298245614035, 0.674053554939982, 0.665053242981607, 0.650793650793651, 
0.745119305856833, 0.747816593886463, 0.743243243243243, 0.795053003533569, 
0.815889029003783, 0.818543046357616, 0.847856154910097, 0.956456456456456, 
0.898773006134969, 0.861325115562404, 0.856910569105691, 0.911917098445596, 
0.887563884156729, 0.869718309859155, 0.958254269449715, 1.01803607214429, 
1.07392197125257, 0.979209979209979, 0.974576271186441, 0.943396226415094, 
0.927194860813705)), row.names = c(NA, -297L), class = c("tbl_df", 
"tbl", "data.frame"))

【问题讨论】:

不幸的是,您的代码不可重现,因为您的数据没有名为 Date 的列。要设置颜色,您可以尝试使用 aes(..., color = case_when(Proporsi &gt;= 0.4 ~ "RED", Proporsi &lt;= 0.2 ~ "GREEN", TRUE ~ "Yellow")) 而不是 if ... else @stefan 谢谢您,我错过了日期列并对其进行了编辑。代码允许折线图的每个段的颜色不同。但我的目标是根据 y 轴的最新值对整条线进行着色。如果您有解决方法,我们将不胜感激!谢谢你! 【参考方案1】:

根据最后一个值为线条着色的一个选项是向您的数据框添加一列,该列包含每个组的最后一个值,可用于设置颜色。要获得正确的颜色,您必须使用scale_color_manual。我不确定你想如何放置标签。在我看来,使用ggrepel 并不是绝对必要的。相反,我使用geom_text 将标签放在最后一个数据点的右侧。为此,我增加了 x 比例的扩展,为标签腾出空间。但请随意切换到ggrepel

library(ggplot2)
library(dplyr)

mydata <- mydata %>% 
  group_by(Provinsi) %>% 
  mutate(
    Proporsi_last = last(Proporsi),
    color = case_when(Proporsi_last >= 0.4 ~ "RED", Proporsi_last <= 0.2 ~ "GREEN", TRUE ~ "YELLOW")
  )

mydata_ends <- mydata %>% 
  group_by(Provinsi) %>% 
  top_n(1, Date)

ggplot(data = mydata, aes(x = Date, y = Proporsi, color = color)) +
  xlab("Tanggal") +
  ylab("Proporsi Keterisian TT/Kasus Aktif") +
  scale_y_continuous(labels = scales::percent_format(accurracy = 1)) +
  scale_color_manual(values = c(RED = "red", GREEN = "green", YELLOW = "yellow")) +
  geom_line() +
  geom_text(aes(label = scales::percent(Proporsi)), data = mydata_ends, size = 3, hjust = -.1, color = "black") +
  scale_x_datetime(expand = expansion(mult = c(.05, .4))) +
  facet_wrap(~Provinsi, ncol = 4) +
  ggtitle("Proporsi KeterisianTT/Kasus Aktif") +
  theme(plot.title = element_text(family = "Trebuchet MS", face = "bold", size = 20, hjust = 0, color = "#555555")) +
  theme(axis.text.x = element_text(angle = 90))

【讨论】:

以上是关于如何根据时间序列折线图上的最后一个值着色?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用文本文件中的数据集在Java中绘制折线图?

线图的matlab颜色条按值着色

如何使用 plotly express 向折线图添加点或标记?

如何使用 JasperReports 6.7.0 在折线图上自定义轴?

echarts图表——折线图&饼图

如何在matplotlib中创建同一条线具有两种不同颜色的折线图?