R 用ggplot绘制图例

R 用ggplot绘制图例,r,dataframe,ggplot2,tidyverse,R,Dataframe,Ggplot2,Tidyverse,我正试图用带有正负条的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 cod

我正试图用带有正负条的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)")
indegree最简单的解决方案:

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

你能具体说明你到底想要什么吗?@denis你能帮我画一个图例(蓝色=indegree;红色=outdegree)可能的重复:@MrFlick legend蓝色=indegree和红色=outdegree@user10974052没有铅。我添加了更合适的代码,以便您可以选择颜色。使用
ggplot
时习惯长格式,更方便、更强大
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
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