我在R中的数据集如下所示:
a <- c("M","F","F","F","M","M","F","F","F","M","F","F","M","M","F")
p <- c("P","P","W","W","P","P","W","W","W","W","P","P","P","W","W")
y1 <- c("yes","yes","null","no","no","no","yes","null","no","yes","yes","yes","null","no","no")
y2 <- c("yes","null","no","no","no","yes","yes","yes","null","no","yes","null","no","yes","yes")
y3 <- c("no","no","no","yes","null","yes","null","no","no","no","yes","yes","null","no","no")
VE <- data.frame(gender = a,
type = p,
y1 = y1,
y2 = y2,
y3 = y3)
我想创建一个如下所示的条形图: ideal bar chart
我想了解图表还有很长的路要走:
q<-data.frame(gender=VE$gender,
year=rep("y1",15),
group=VE$y1)
p<-data.frame(gender=VE$gender,
year=rep("y2",15),
group=VE$y2)
x<-data.frame(gender=VE$gender,
year=rep("y3",15),
group=VE$y3)
Table<-rbind(q,p,x)
ggplot(Table, aes(year)) + geom_bar(aes(fill=group), position = "stack") + facet_grid(gender~.)
有没有更好的方法来获得条形图? (因为我本来打算处理3,000,000个每个都有32个变量的观察) 请给我一些这个条形图的帮助。干杯!
答案 0 :(得分:0)
首先,您可以融化data.frame以获得“长”格式。为此,我创建了一个ID变量,将3个变量'y1,'y2'和'y3'组合成一个变量。
然后,您可以使用geom_bar()
并使用x
,如果没有提供y
审美,则会计算library(ggplot2)
# create data frame
df <- data.frame(ID = 1:15,
gender = c('M', 'F', 'F', 'F', 'M', 'M', 'F', 'F', 'F', 'M', 'F', 'F', 'M', 'M', 'F'),
type = toupper(c('p', 'p', 'w', 'w', 'p', 'p', 'w', 'w', 'w', 'w', 'p', 'p', 'p', 'W', 'W')),
y1 = c('yes', 'yes', 'null', 'no', 'no', 'no', 'yes', 'null', 'no', 'yes', 'yes', 'yes', 'null', 'no', 'no'),
y2 = c('yes', 'null', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'null', 'no', 'yes', 'null', 'no', 'yes', 'yes'),
y3 = c('no', 'no', 'no', 'yes', 'null', 'yes', 'null', 'no', 'no', 'no', 'yes', 'yes', 'null', 'no', 'no'),
stringsAsFactors = TRUE)
# melt data frame to long format
df_melt <- data.table::melt(df[, c(1, 4:6)], id.vars = "ID")
# set correct levels for factor (needed for the legend)
df_melt$value <- factor(df_melt$value, levels = c("yes", "no", "null"))
# add ggplot
ggplot(data = df_melt) +
geom_bar(aes(x = variable, fill = value, colour = value)) +
ylab("count") +
xlab("year")
美学中的值。
class Organization(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
...
返回: