Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

r - How to show the name of the observation on the boxplot in ggplot2?

Suppose you have a dataframe with company names, indicators of effeciency and their values. I want to visualise the distribution of the values by indicators. Moreover, I want to show the names of companies on the boxplot.

I succeeded in making the boxplot. However, I also tried to use geom_text(label = ...) to show the names of my companies. And it is okay until I am trying to show only one name of particular company.

The question: How do I depict only one company name on the boxplot?

Sample data:

structure(list(comp_name = c("Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", "Andrew", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter", "Peter", "Peter", "Peter", 
"Peter", "Peter", "Peter", "Peter"), indicator = c("F1", "F1", 
"F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", 
"F1", "F1", "F1", "F1", "F1", "F1", "F1", "F2", "F2", "F2", "F2", 
"F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", 
"F2", "F2", "F2", "F2", "F2", "F3", "F3", "F3", "F3", "F3", "F3", 
"F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", 
"F3", "F3", "F3", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", 
"F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", "F1", 
"F1", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", 
"F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F2", "F3", 
"F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3", 
"F3", "F3", "F3", "F3", "F3", "F3", "F3", "F3"), value = c(4.52116335801444, 
6.98749208273682, 4.06677492882586, 5.15558916506456, 2.55021540348946, 
4.03979028008168, 3.751515686978, 5.08003125851194, 8.7555968159682, 
5.4394771555829, 1.19766306732289, 5.93147731602626, 5.59646604833006, 
1.73138645071719, 5.11918222532415, 5.83342701888127, 4.3063821275868, 
5.46551649254734, 2.03791096436598, 6.78824196220435, 7.06567057699402, 
2.12718564271759, 2.2767564436691, 2.9340306023389, 2.79354137930189, 
1.74012197713116, 4.63834192235623, 4.92206445766098, 5.92405081403378, 
8.09782132090054, 6.97285432280469, 5.07130341848169, 3.30182914153493, 
3.34397253870246, 3.46478566548551, 2.3993549803844, 2.57123338842713, 
5.3477268015452, 4.91017847933986, 3.06690395074572, 4.88326550588307, 
6.14840924478227, 3.03774145148148, 2.15503416860755, 2.59633674164204, 
4.42310726849601, 2.13363689471252, 0.456381227308591, 5.691163348962, 
0.912347547789089, 5.77435176159163, 4.07402763667729, 3.82057336446912, 
3.34397163555984, 3.29222606327468, 6.16552682906508, 4.35344041700362, 
7.67032346030189, 3.26348961010125, 5.25322571330987, 3.66694544697633, 
6.0361925048948, 2.88873148939352, 8.45661226220137, 7.87329217470112, 
2.70345782748427, 6.91511647510829, 1.54134508545522, 2.18208998284389, 
3.16124793548385, 4.31666612130624, 4.54958484376762, 8.50892596634577, 
7.5225632222216, 4.58217957515625, 4.08628383752229, 6.77539922377002, 
7.78460326932369, 6.25295512473375, 5.48269676141167, 3.84114520608033, 
2.41963345063092, 2.912374298312, 7.17268035511083, 3.81530974737139, 
1.80282982429818, 3.50746999074847, 5.15224746117473, 5.18355688999888, 
5.47519426645026, 9.87303057407119, 6.07147247898117, 3.19602380164489, 
2.59514592800692, 7.35458770398942, 5.4947663156565, 5.90316497585236, 
5.68887350708009, 0.992336549260927, 5.40819995742745, 2.75688036687868, 
5.30089244576671, 0.623445829887006, 5.10340466234114, 3.73065932502235, 
3.75666881288993, 1.7474419966562, 4.32062928836251, 6.72395389095317, 
1.69971729442571, 6.77489190923027, 5.34359382756765, 2.65431117498653, 
2.33790746229386, 3.4362010025597, 4.67710789802222, 4.7103208700674, 
5.78846064996488, 7.23020019917477, 4.07601609654997)), class = "data.frame", row.names = c(NA, 
-120L))

What I tried to do:

ggplot(sampleData, aes(x = indicator, y = value)) +
  geom_boxplot() + geom_text(label = comp_name)
question from:https://stackoverflow.com/questions/66068734/how-to-show-the-name-of-the-observation-on-the-boxplot-in-ggplot2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Alright, the answer was on the surface.

Found here: https://www.r-graph-gallery.com/275-add-text-labels-with-ggplot2.html

If you want to put one or several labels, use geom_text or geom_label and filter your dataframe as you want. Like this

    ggplot(data, aes(x = factor, y = value)) + 
       geom_boxplot() + 
       geom_label(data = data %>% filter(indicator == "f1"), aes(label = indicator))

You even can make your custom lables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...