Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

How to distinguish between overlapping data labels?

$
0
0

My data labels overlap on my plot and even after using ggrepel it still occurs. Is there a way to re-position specific data labels? The problem is that each point has been coloured and if that point overlaps I cannot tell which group its being grouped in. Can I show even those data labels that overlap on the graph by another way other than colour? because it seems colour is inefficient when overlapping that even data_text_repel can save from.

An example of my data (LARGE):

structure(list(Month = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), .Label = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec"), class = "factor"), Year = c(1994L, 1994L, 1994L, 
1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 
1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 1995L, 
1995L, 1995L, 1995L, 1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 
1996L, 1996L, 1996L, 1996L, 1996L, 1996L, 1997L, 1997L, 1997L, 
1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 1997L, 
1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 1998L, 
1998L, 1998L, 1998L, 1999L, 1999L, 1999L, 1999L, 1999L, 1999L, 
1999L, 1999L, 1999L, 1999L, 1999L, 1999L, 2000L, 2000L, 2000L, 
2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 
2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 
2002L, 2002L, 2002L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2004L, 2004L, 2004L, 
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 
2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 
2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 
2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 
2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 
2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 
2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 
2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 
2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 
2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 
2011L, 2011L, 2011L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 
2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L
), Temperature = c(5.1137927578405, 3.21683318428133, 7.78435180363612, 
8.37781037953335, 10.9711757486259, 15.0639699124053, 19.0679255131, 
16.8439513657227, 13.2844947714186, 10.1717660661212, 9.87512852747182, 
6.23880410693382, 4.39990197256963, 6.37256580684349, 5.57871937517377, 
9.0978086365033, 11.8357026274332, 13.8524686214474, 19.1933904027113, 
19.1113251384721, 13.9094592622846, 13.1400688233997, 7.57577998884861, 
2.03480418222265, 3.93035220278974, 2.23476439014773, 3.9901885352985, 
8.49065701936191, 9.15289067652546, 14.80103145439, 16.8660647663761, 
16.9684443134142, 13.7884950559379, 11.5001860584192, 5.67129848728839, 
2.87373159251973, 1.7808907070319, 6.49539784093825, 8.3344507659725, 
8.59164858808079, 11.7412069862402, 14.6023515839348, 16.8972350474694, 
19.9699405174499, 14.791330044166, 10.1975137511312, 8.27008113706325, 
5.67278318819756, 5.13795257533121, 6.86800188715349, 7.829852949304, 
8.19167119234131, 12.9267936638086, 14.8737114943895, 15.9960651912575, 
16.4659529750388, 14.9436075278718, 10.494000768083, 5.19573974906802, 
5.47844832135603, 5.52247869520634, 4.73977971251722, 7.51569245950698, 
9.67338886226658, 13.3184710778825, 14.0355257426871, 18.0932927319341, 
16.8136169272658, 16.3574227733512, 10.859327203119, 7.64878507305478, 
4.49344395718576, 4.76352759282398, 5.99710312508096, 7.30866258897677, 
8.21490506412456, 12.4355378315249, 15.57831699222, 15.2836110023306, 
17.4285121275592, 15.5563940243565, 10.644136026412, 6.97188308342617, 
5.72719122612059, 5.43459377399137, 7.0905183508297, 7.61273439955019, 
9.56268935385892, 12.1637224025468, 15.0286310800525, 16.7485731660109, 
17.8184385668278, 14.5854724081786, 10.214487092635, 8.46414151966925, 
5.92030114331737, 4.21638714375256, 3.97151295624939, 7.33578094332027, 
9.55507283803179, 12.4172525011993, 16.6448235861242, 18.1237393994832, 
18.9624528277786, 14.6215361632556, 9.36684051091755, 8.35554865608939, 
4.92668967156987, 4.9340017349914, 5.36982772412438, 6.70912745111707, 
9.62498511796711, 12.150353152089, 15.885594984704, 16.5012386614953, 
18.4021460822011, 15.3754094657338, 11.1612987107906, 7.64558785383179, 
4.99559395947075, 5.80950180711545, 4.12769306249583, 7.04234985523857, 
9.50780910851013, 11.8560465253314, 15.9793910691043, 17.0945962814144, 
16.499843974081, 15.9806579475095, 13.8462430445877, 6.37511283619506, 
4.36643441024687, 4.35598184684807, 4.07781454209256, 5.07580052442755, 
8.94935890789865, 12.9095780367143, 16.3518289806354, 20.7742600040926, 
16.6601687584144, 17.8009874515646, 13.7700907514365, 8.44016520383474, 
6.49344001987084, 7.12818708453161, 6.09998890983074, 7.59431488410382, 
11.5160870770962, 12.5384011591965, 15.6577982450023, 16.4787832488391, 
16.2383245979215, 14.677549085112, 11.0894576864184, 7.07639044291793, 
5.28077322301684, 6.80543525213961, 5.54080559385412, 6.38462907615164, 
8.63505291385181, 13.5803791162643, 15.082631660556, 17.2451509228625, 
17.3973227419433, 13.9849296684597, 9.85791003192315, 7.28958606582769, 
3.79704691416784, 2.85030455713634, 4.27741292127206, 7.18508457896512, 
10.6726843261928, 13.0402461661106, 15.5194182496581, 17.1389941286013, 
17.9852938381448, 15.1061925016421, 11.7400657343966, 8.98079493377833, 
3.23752886908435, 1.38218920882819, 2.95314428136539, 6.53618709513281, 
9.21967752328368, 10.9704434019157, 15.7269035259945, 18.7383796516359, 
16.2546786674278, 14.2113346745367, 10.8649438667867, 5.44778841099725, 
0.0320333532038124, 4.32586417658113, 6.46658541913653, 6.84301280723963, 
12.2416812213663, 13.1105952317722, 14.8318448373076, 15.6486184646722, 
16.25518817037, 15.8956912766942, 13.0182212357672, 9.77060116293248, 
5.77505121430355, 5.86511563894169, 4.14679034429081, 8.38983990645603, 
8.04163809881342, 12.4681630134688, 14.3173467688431, 16.2623240334122, 
17.945965451425, 13.9994464698033, 10.4153188083713, 7.24843214155896, 
5.07810471099195, 3.54166100139566, 3.18987592109164, 3.16540216834855, 
8.03540086651316, 10.8485250888494, 14.241571176026, 18.8873677159289, 
18.2152274340065, 14.5218838159797, 13.1458883824803, 6.85796723861855, 
6.31980104716313)), row.names = c(NA, -228L), class = "data.frame")

Code used:

year_summ = prac1 %>%
  group_by(Year) %>%
  arrange(Temperature) %>%
  slice(c(1, n())) %>%
  mutate(type = c("Min Year", "Max Year"))

month_summ = prac1 %>%
  group_by(Month) %>%
  arrange(Temperature) %>%
  slice(c(1, n())) %>%
  mutate(type = c("Min Month", "Max Month"))

prac1_summ = bind_rows(year_summ, month_summ) %>%
  mutate(type = factor(type, levels = c("Min Month", "Max Month", "Min Year", "Max Year"))) %>%
  arrange(type)
aggregate(Temperature ~ Month + Year + type, prac1_summ, mean(round(Temperature, 2)))

My try with Plot code:

ggplot(prac1, aes(x = Month, Temperature, group = Year)) + 
  geom_line() +
  geom_point(data = prac1_summ, aes(color = type)) +
  scale_color_manual(values = c("red", "blue", "yellow", "green")) +
  facet_wrap(~Year) + geom_text_repel(data = prac1_summ, aes(label=round(Temperature,2)), nudge_y = -5) +
  theme_bw() +  theme(panel.grid.major = element_blank(),
                      panel.grid.minor = element_blank(),
                      strip.background = element_blank(),
                      panel.border = element_rect(colour = "black"))

And outcome: enter image description here


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>