用ggplot绘制图例

时间:2019-02-04 15:34:39

标签: r dataframe ggplot2 tidyverse

我正在尝试用ggplot2绘制带正负条的带标签的barplot。

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))

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

enter image description here

1 个答案:

答案 0 :(得分: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