用ggplot绘制图例

用户名

我正在尝试使用带有正负条的ggplot2绘制带标签的条形图。

indegree<- c("Volunteer work and meeting (1 code)", "TV and Video (8 codes)", "Travel by purpose (30 codes)", "Travel and unspecified time use (1 code)", "Study (1 code)",
             "Sports related activites (3 codes)", "Sports and outdoor activities (1 code)","Social life (6 codes)", "Social care(1 code)", "Shopping and services (11 codes)",
             "Second Job (2 codes)","School or University (4 codes)", "Resting-Time out (1 code)",  "Reading (4 codes)", "Radio and Music (6 codes)", "Games (9 codes)",
             "Physical exercise (20 codes)","Personal care (1 cdoe)",  "Participatory activities (4 codes)","Other personal care (3 codes)","Organisational work (4 codes)",
             "Mass media (1 code)", "Making care for textiles (5 codes)","Main job (3 codes)", "Sleep (3 codes)", "Informal help to other households  (23 codes)", "Household upkeep (7 codes)",
             "Household management (10 codes)", "Household and family care (1 code)", "Hobbies, games and computing (1 code)", "Help to an adult household member", 
             "Gardening and pet care (5 codes)", "Free time study (1 code)", "Food Management (5 codes)", "Entertainment and culture (25 codes)",  "Employment(1 code)",
             "Eating (1 code)", "Construction and repairs (8 codes)",  "Computing (10 codes)","Childcare of own household members (8 codes)", "Arts and hobbies (14 codes)", 
             "Activities related to employment (5 codes)","Punctuating activity (eg. Unspecified time use, 8 codes)")

创建度数值

indegreevalues <- as.numeric(c(3, 35, 37, 26, 3, 5, 4, 36, 2, 29, 4, 23, 31, 29, 23, 28, 33, 5, 22, 37, 15, 12, 28, 28, 32, 25, 37, 26,
                                           22,4,16,30,11,34,22,5,35,26,33,26,22,16, 35))
df.1 <- data.frame(indegree, indegreevalues) #created a data frame

创建数据框2

outdegree<- c("Volunteer work and meeting (1 code)", "TV and Video (8 codes)", "Travel by purpose (30 codes)", "Travel and unspecified time use (1 code)", "Study (1 code)",
             "Sports related activites (3 codes)", "Sports and outdoor activities (1 code)","Social life (6 codes)", "Social care(1 code)", "Shopping and services (11 codes)",
             "Second Job (2 codes)","School or University (4 codes)", "Resting-Time out (1 code)",  "Reading (4 codes)", "Radio and Music (6 codes)", "Games (9 codes)",
             "Physical exercise (20 codes)","Personal care (1 cdoe)",  "Participatory activities (4 codes)","Other personal care (3 codes)","Organisational work (4 codes)",
             "Mass media (1 code)", "Making care for textiles (5 codes)","Main job (3 codes)", "Sleep (3 codes)", "Informal help to other households  (23 codes)", "Household upkeep (7 codes)",
             "Household management (10 codes)", "Household and family care (1 code)", "Hobbies, games and computing (1 code)", "Help to an adult household member", 
             "Gardening and pet care (5 codes)", "Free time study (1 code)", "Food Management (5 codes)", "Entertainment and culture (25 codes)",  "Employment(1 code)",
             "Eating (1 code)", "Construction and repairs (8 codes)",  "Computing (10 codes)","Childcare of own household members (8 codes)", "Arts and hobbies (14 codes)", 
             "Activities related to employment (5 codes)","Punctuating activity (eg. Unspecified time use, 8 codes)")

创建学位

outdegreevalues<- as.numeric(c(30, 25, 5, 28, 22, 12, 35, 35, 30, 32, 25, 16, 34, 4, 3, 1, 24, 34, 28, 32, 19, 8, 23, 21, 30, 4, 30, 35,
                                           35,23,23,30,27,25,39,39,20,29,4,12,4,15, 5))

创建一个新的数据框

df.2 <- data.frame(outdegree, outdegreevalues) #created second df
df <- merge(df.1,df.2,by.x = "indegree",by.y = "outdegree")

绘制合并的数据帧

p<-ggplot(df) + 
  geom_bar(aes(x=indegree,y=indegreevalues),fill="blue",stat="identity",position="dodge") + 
  geom_bar(aes(x=indegree,y=-outdegreevalues),fill="red",stat="identity",position="dodge")+ 
  labs(x="Activities", y="Count", fill="") + 
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90, hjust = 1))

但是我在绘制图例时遇到了问题。你能帮我画个图例吗

在此处输入图片说明

丹尼斯

最简单的解决方案:

p<-ggplot(df) + 
  geom_bar(aes(x=indegree,y=indegreevalues,fill = "in"),stat="identity",position="dodge") + 
  geom_bar(aes(x=indegree,y=-outdegreevalues,fill = "out"),stat="identity",position="dodge")+ 
  labs(x="Activities", y="Count", fill="")+ 
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90, hjust = 1))+
p

但是,您不能用它scale_color_manual来选择我认为的颜色。好的做法ggplot是长格式。因此,这里以data.table为例重塑数据:

library(data.table)
p<-ggplot(melt(as.data.table(df)[,outdegreevalues := -outdegreevalues],measure.vars = c("indegreevalues","outdegreevalues"))) + 
  geom_bar(aes(x=indegree,y=value,fill = variable),stat="identity",position="dodge") + 
  labs(x="Activities", y="Count", fill="")+ 
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 90, hjust = 1))+
  scale_color_manual(values=c("red","blue"))
p

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章