为R中的大数据集的条形图ggplot添加标签(计数)

u3r8eeie  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(109)

我有一个测试的汽车和数字的条形图,我试图将每辆汽车的计数添加到条形图上,但数字只是聚集在条形图的底部,下面是我的代码

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", "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", "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", "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, 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),
请注意,计数(总计)刻度最大为200,000
谢谢

nzkunb0c

nzkunb0c1#

当你使用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)

它将提供以下输出:

下一次我建议上传完整的代码,导致您目前正在生产的图形。我没有您的“计数”标签,但一个负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", "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)

确认你的问题更多的是关于重复的值而不是hjust(顺便说一句,hjust应该在-1和0之间,而不是-10)。

注意我在数据中添加了一些品牌,这不是一个值得关注的问题,因为你有很多品牌的数据。希望它能解决你的问题!

相关问题