英文:
Add labels (count) to bar plot ggplot for large dataset in R
问题
以下是您代码中需要翻译的部分:
cars %>%
mutate(Brand = fct_reorder(Brand, Total-test, .fun = sum)) %>%
ggplot(aes(x = Brand, y = Total)) +
geom_bar(stat = "identity", fill = "blue", width = 0.4) +
coord_flip() +
geom_text(aes(label = Total-test), hjust = -10)
请注意,您提到的 "Total" 刻度高达 200,000。
感谢。
英文:
So I have a barplot of cars and numbers tested and I'm trying to add the counts of each car on it's bar but the numbers are just clustered at the foot of the bar, below is my code
cars %>%
mutate(Brand = fct_reorder(Brand,Total-test,.fun = sum)) %>%
ggplot(aes(x=Brand, y=Total)) +
geom_bar(stat="identity", fill="blue",width=0.4) +
coord_flip()+
geom_text(aes(label=Total-test), hjust=-10)
dput(head(cars,50))
structure(list(vehicle = c("ALFA ROMEO-147-2003", "ALFA ROMEO-147-2004",
Vehicle Make
"ALFA ROMEO-147-2005", "ALFA ROMEO-147-2006", "ALFA ROMEO-147-2007",
"ALFA ROMEO-147-2008", "ALFA ROMEO-147-2010", "ALFA ROMEO-155-1997",
"ALFA ROMEO-156-1999", "ALFA ROMEO-156-2000", "ALFA ROMEO-156-2001",
"ALFA ROMEO-156-2002", "ALFA ROMEO-156-2003", "ALFA ROMEO-156-2004",
"ALFA ROMEO-156-2005", "ALFA ROMEO-159-2006", "ALFA ROMEO-159-2007",
"ALFA ROMEO-159-2008", "ALFA ROMEO-159-2009", "ALFA ROMEO-159-2010",
"ALFA ROMEO-159-2011", "ALFA ROMEO-166-2004", "ALFA ROMEO-BRERA-2006",
"ALFA ROMEO-BRERA-2007", "ALFA ROMEO-BRERA-2008", "ALFA ROMEO-GIULIA-2017",
"ALFA ROMEO-GIULIETTA-2010", "ALFA ROMEO-GIULIETTA-2011", "ALFA ROMEO-GIULIETTA-2012",
"ALFA ROMEO-GIULIETTA-2013", "ALFA ROMEO-GIULIETTA-2014", "ALFA ROMEO-GIULIETTA-2015",
"ALFA ROMEO-GIULIETTA-2016", "ALFA ROMEO-GIULIETTA-2017", "ALFA ROMEO-GT-2005",
"ALFA ROMEO-GT-2006", "ALFA ROMEO-GT-2007", "ALFA ROMEO-GT-2008",
"ALFA ROMEO-GT-2009", "ALFA ROMEO-GT-2010", "ALFA ROMEO-MITO-2009",
"ALFA ROMEO-MITO-2010", "ALFA ROMEO-MITO-2011", "ALFA ROMEO-MITO-2017",
"ALFA ROMEO-OTHER-1996", "ALFA ROMEO-OTHER-2001", "ALFA ROMEO-OTHER-2002",
"ALFA ROMEO-OTHER-2003", "ALFA ROMEO-OTHER-2009", "ALFA ROMEO-OTHER-2010"
), = c("ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
Vehicle Model
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO",
"ALFA ROMEO", "ALFA ROMEO"),= c("147", "147",
Year Of Birth
"147", "147", "147", "147", "147", "155", "156", "156", "156",
"156", "156", "156", "156", "159", "159", "159", "159", "159",
"159", "166", "BRERA", "BRERA", "BRERA", "GIULIA", "GIULIETTA",
"GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA",
"GIULIETTA", "GIULIETTA", "GT", "GT", "GT", "GT", "GT", "GT",
"MITO", "MITO", "MITO", "MITO", "OTHER", "OTHER", "OTHER", "OTHER",
"OTHER", "OTHER"),= c(2003, 2004, 2005, 2006,
PASS %
2007, 2008, 2010, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2006, 2007, 2008, 2009, 2010, 2011, 2004, 2006, 2007, 2008, 2017,
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2005, 2006, 2007,
2008, 2009, 2010, 2009, 2010, 2011, 2017, 1996, 2001, 2002, 2003,
2009, 2010), Total = c(9, 12, 19, 24, 43, 46, 13, 7, 9, 7, 22,
8, 37, 42, 21, 40, 123, 164, 83, 56, 27, 6, 9, 14, 10, 94, 17,
92, 15, 10, 13, 25, 8, 25, 35, 40, 45, 51, 12, 10, 43, 26, 33,
7, 6, 16, 10, 7, 32, 45), PASS = c(2, 3, 6, 8, 10, 23, 4, 3,
4, 0, 5, 1, 12, 12, 6, 17, 42, 56, 30, 20, 13, 0, 3, 3, 6, 56,
10, 58, 8, 9, 11, 22, 5, 23, 13, 11, 16, 16, 3, 3, 26, 14, 17,
5, 4, 7, 5, 2, 18, 16), = c(22.2, 25, 31.6, 33.3, 23.3,
50, 30.8, 42.9, 44.4, 0, 22.7, 12.5, 32.4, 28.6, 28.6, 42.5,
34.1, 34.1, 36.1, 35.7, 48.1, 0, 33.3, 21.4, 60, 59.6, 58.8,
63, 53.3, 90, 84.6, 88, 62.5, 92, 37.1, 27.5, 35.6, 31.4, 25,
30, 60.5, 53.8, 51.5, 71.4, 66.7, 43.8, 50, 28.6, 56.3, 35.6),
please note the count(Total) scale is up to 200,000
Thanks
答案1
得分: 2
使用ggplot制作条形图时,您可以在vjust中使用负数来设置名称。如果在"coord_flip"之后使用这个参数,您需要将这个参数更改为hjust。以下是一个使用可重现数据的示例,我在这些分钟内制作的:
library("dplyr")
library("ggplot2")
library("forcats")
data(mtcars)
cars <- as.data.frame(cbind(mtcars, rownames(mtcars)))
names(cars)[names(cars) == 'rownames(mtcars)'] <- 'Brand'
cars %>%
ggplot(aes(x = Brand, y = wt)) +
geom_bar(stat = "identity", fill = "blue", width = 0.4) +
coord_flip() +
geom_text(aes(label = Brand), hjust = -0.2)
这将为您提供以下输出:
下次建议上传导致您当前产生的图形的完整代码。我没有您的"count"标签,但负的hjust应该有效。
在评论之后,我做了以下更改:
library("dplyr")
library("ggplot2")
library("forcats")
data(mtcars)
vehicle = c("ALFA ROMEO-147-2003", "ALFA ROMEO-147-2004", "ALFA ROMEO-147-2005", "ALFA ROMEO-147-2006", "ALFA ROMEO-147-2007", "ALFA ROMEO-147-2008", "ALFA ROMEO-147-2010", "ALFA ROMEO-155-1997", "ALFA ROMEO-156-1999", "ALFA ROMEO-156-2000", "ALFA ROMEO-156-2001", "ALFA ROMEO-156-2002", "ALFA ROMEO-156-2003", "ALFA ROMEO-156-2004", "ALFA ROMEO-156-2005", "ALFA ROMEO-159-2006", "ALFA ROMEO-159-2007", "ALFA ROMEO-159-2008", "ALFA ROMEO-159-2009", "ALFA ROMEO-159-2010", "ALFA ROMEO-159-2011", "ALFA ROMEO-166-2004", "ALFA ROMEO-BRERA-2006", "ALFA ROMEO-BRERA-2007", "ALFA ROMEO-BRERA-2008", "ALFA ROMEO-GIULIA-2017", "ALFA ROMEO-GIULIETTA-2010", "ALFA ROMEO-GIULIETTA-2011", "ALFA ROMEO-GIULIETTA-2012", "ALFA ROMEO-GIULIETTA-2013", "ALFA ROMEO-GIULIETTA-2014", "ALFA ROMEO-GIULIETTA-2015", "ALFA ROMEO-GIULIETTA-2016", "ALFA ROMEO-GIULIETTA-2017", "ALFA ROMEO-GT-2005", "ALFA ROMEO-GT-2006", "ALFA ROMEO-GT-2007", "ALFA ROMEO-GT-2008", "ALFA ROMEO-GT-2009", "ALFA ROMEO-GT-2010", "ALFA ROMEO-MITO-2009", "ALFA ROMEO-MITO-2010", "ALFA ROMEO-MITO-2011", "ALFA ROMEO-MITO-2017", "ALFA ROMEO-OTHER-1996", "ALFA ROMEO-OTHER-2001", "ALFA ROMEO-OTHER-2002", "ALFA ROMEO-OTHER-2003", "ALFA ROMEO-OTHER-2009", "ALFA ROMEO-OTHER-2010")
Vehicle_Make = c("ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "BMW", "ALFA ROMEO", "Audi", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "Audi", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "BMW", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "Audi", "BMW", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO")
Vehicle_Model = c("147", "147", "147", "147", "147", "147", "147", "155", "156", "156", "156", "156", "156", "156", "156", "159", "159", "159", "159", "159", "159", "166", "BRERA", "BRERA", "BRERA", "GIULIA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GT", "GT", "GT", "GT", "GT", "GT", "MITO", "MITO", "MITO", "MITO", "OTHER", "OTHER", "OTHER", "OTHER", "OTHER", "OTHER")
Year_Of_Birth = c(2003, 2004, 2005, 2006, 2007, 2008, 2010, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2004, 2006, 2007, 2008, 2017, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2005, 2006, 200
<details>
<summary>英文:</summary>
When you use ggplot to make a bar chart, you can set the name with a negative number in the vjust. In case you put this argument after the "coord_flip", you need to change this argument to hjust. Here is an example with the reproducible data I was able to make in these minutes:
library("dplyr")
library("ggplot2")
library("forcats")
data(mtcars)
cars <- as.data.frame(cbind(mtcars,rownames(mtcars)))
names(cars)[names(cars) == 'rownames(mtcars)'] <- 'Brand'
cars %>%
ggplot(aes(x=Brand, y=wt)) +
geom_bar(stat="identity", fill="blue",width=0.4) +
coord_flip()+
geom_text(aes(label=Brand), hjust=-0.2)
Which gives you the following output:
[![enter image description here][1]][1]
For next time I suggest uploading the complete code that leads to the graphic you are currently producing. I don't have your "count" label, but a negative hjust should work.
---------------------------------------------------------------------------
After comments i did this:
library("dplyr")
library("ggplot2")
library("forcats")
data(mtcars)
vehicle = c("ALFA ROMEO-147-2003", "ALFA ROMEO-147-2004", "ALFA ROMEO-147-2005", "ALFA ROMEO-147-2006", "ALFA ROMEO-147-2007", "ALFA ROMEO-147-2008", "ALFA ROMEO-147-2010", "ALFA ROMEO-155-1997", "ALFA ROMEO-156-1999", "ALFA ROMEO-156-2000", "ALFA ROMEO-156-2001", "ALFA ROMEO-156-2002", "ALFA ROMEO-156-2003", "ALFA ROMEO-156-2004", "ALFA ROMEO-156-2005", "ALFA ROMEO-159-2006", "ALFA ROMEO-159-2007", "ALFA ROMEO-159-2008", "ALFA ROMEO-159-2009", "ALFA ROMEO-159-2010", "ALFA ROMEO-159-2011", "ALFA ROMEO-166-2004", "ALFA ROMEO-BRERA-2006", "ALFA ROMEO-BRERA-2007", "ALFA ROMEO-BRERA-2008", "ALFA ROMEO-GIULIA-2017", "ALFA ROMEO-GIULIETTA-2010", "ALFA ROMEO-GIULIETTA-2011", "ALFA ROMEO-GIULIETTA-2012", "ALFA ROMEO-GIULIETTA-2013", "ALFA ROMEO-GIULIETTA-2014", "ALFA ROMEO-GIULIETTA-2015", "ALFA ROMEO-GIULIETTA-2016", "ALFA ROMEO-GIULIETTA-2017", "ALFA ROMEO-GT-2005", "ALFA ROMEO-GT-2006", "ALFA ROMEO-GT-2007", "ALFA ROMEO-GT-2008", "ALFA ROMEO-GT-2009", "ALFA ROMEO-GT-2010", "ALFA ROMEO-MITO-2009", "ALFA ROMEO-MITO-2010", "ALFA ROMEO-MITO-2011", "ALFA ROMEO-MITO-2017", "ALFA ROMEO-OTHER-1996", "ALFA ROMEO-OTHER-2001", "ALFA ROMEO-OTHER-2002", "ALFA ROMEO-OTHER-2003", "ALFA ROMEO-OTHER-2009", "ALFA ROMEO-OTHER-2010" )
Vehicle_Make = c("ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "BMW", "ALFA ROMEO", "Audi", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "Audi", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "BMW", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "Audi", "BMW", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO", "ALFA ROMEO")
Vehicle_Model = c("147", "147", "147", "147", "147", "147", "147", "155", "156", "156", "156", "156", "156", "156", "156", "159", "159", "159", "159", "159", "159", "166", "BRERA", "BRERA", "BRERA", "GIULIA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GIULIETTA", "GT", "GT", "GT", "GT", "GT", "GT", "MITO", "MITO", "MITO", "MITO", "OTHER", "OTHER", "OTHER", "OTHER", "OTHER", "OTHER")
Year_Of_Birth= c(2003, 2004, 2005, 2006, 2007, 2008, 2010, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2004, 2006, 2007, 2008, 2017, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2005, 2006, 2007, 2008, 2009, 2010, 2009, 2010, 2011, 2017, 1996, 2001, 2002, 2003, 2009, 2010)
Total = c(9, 12, 19, 24, 43, 46, 13, 7, 9, 7, 22, 8, 37, 42, 21, 40, 123, 164, 83, 56, 27, 6, 9, 14, 10, 94, 17, 92, 15, 10, 13, 25, 8, 25, 35, 40, 45, 51, 12, 10, 43, 26, 33, 7, 6, 16, 10, 7, 32, 45)
PASS = c(2, 3, 6, 8, 10, 23, 4, 3, 4, 0, 5, 1, 12, 12, 6, 17, 42, 56, 30, 20, 13, 0, 3, 3, 6, 56, 10, 58, 8, 9, 11, 22, 5, 23, 13, 11, 16, 16, 3, 3, 26, 14, 17, 5, 4, 7, 5, 2, 18, 16)
PASS_percentage = c(22.2, 25, 31.6, 33.3, 23.3, 50, 30.8, 42.9, 44.4, 0, 22.7, 12.5, 32.4, 28.6, 28.6, 42.5, 34.1, 34.1, 36.1, 35.7, 48.1, 0, 33.3, 21.4, 60, 59.6, 58.8, 63, 53.3, 90, 84.6, 88, 62.5, 92, 37.1, 27.5, 35.6, 31.4, 25, 30, 60.5, 53.8, 51.5, 71.4, 66.7, 43.8, 50, 28.6, 56.3, 35.6)
cars <- as.data.frame(cbind(Vehicle_Make, Total)) %>% mutate(Total=as.double(Total))
cars <- cars %>%
group_by(Vehicle_Make) %>%
summarise(Total = sum(Total))
cars %>% mutate(Brand = fct_reorder(Vehicle_Make,Total,.fun = sum)) %>% ggplot(aes(x=Brand, y=Total)) +
geom_bar(stat="identity", fill="blue",width=0.4) +
coord_flip()+
geom_text(aes(label=Total), hjust=-0.1)
Confirming your problem was more about repeated values than the hjust (which, btw, should be between -1 and 0, not -10). Obtaining this, you should be able to replicate:
[![enter image description here][2]][2]
Notice I added some Brands at hand in the data, not a concerning issue Because you have the data with many of them. Hope it solves your issue!
[1]: https://i.stack.imgur.com/lrxvg.png
[2]: https://i.stack.imgur.com/nqIbv.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论