使用ggplot2创建一个没有设置“y”的折线图?

时间:2017-11-16 20:36:30

标签: r ggplot2

我想我正在尝试创建geom_bar的geom_line版本。我想做行的原因是因为我输入

geom_bar(aes(fill = Decile), position = position_dodge())

我被困在十段中,我的条形图看起来非常混乱。我有11个单独的x变量穿过底部。
问题是我不知道如何将计数用作“y”变量,并尝试了..count..和其他类似的东西,但我完全迷失了。有任何想法吗?

<小时/> 谢谢你的帮助!

我的数据如下:

名称Decile Division
乔1圣地亚哥
1月1日纽约 杰伊2圣地亚哥
Lue 3 Dallas
苏兹2西雅图
tye 3 Dallas

MCD <- read.csv("Decile15.csv", header = TRUE)

MCD$MonthNo <- factor(MCD$MonthNo, levels = c(1:11), labels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November"))

Decile_names <- c('1' = "Decile #1",
                '2' = "Decile #2",
                '3' = "Decile #3",
                '4' = "Decile #4",
                '5' = "Decile #5"
                 )





MCDGraph <- ggplot(na.omit(MCD), aes(MonthNo))

MCDGraph + geom_bar(aes(fill = Division), color = "black", position = "fill") +     facet_wrap(~Decile, nrow = 1, labeller = labeller(Decile = as_labeller(Decile_names))) + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +     scale_fill_manual(values = c("#DA4424", "#24A0DA", "#F0BC0B", "#43F749", "#4348F7", "#F74369", "#D7B9F5")) + theme(panel.background = element_rect(colour = "black", fill = "white"), panel.grid.minor = element_line(color = "black", size = .5)) + labs(x = "2017", y = "% of Leads Per Month By Division") + scale_y_continuous(labels = percent_format()) + theme(strip.background = element_blank(), strip.text = element_text(size = 25))

这是我的分组facet_wrap条形图的百分比。

我将它们分成5个,这样我就可以在一张pdf表上看到5个图而不是10个图。此外,这只分为7个总分。我想做一个分为10的例子。这里有一个例子,我常常把我的分组定做而没有任何分面。

MC <- read.csv("2017_Full_year.csv", header = TRUE)

MC$MonthNo <- factor(MC$MonthNo, levels = c(1:11), labels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November"))



MCH <- ggplot(na.omit(MC), aes(MonthNo))

MCH + geom_bar(aes(fill = Division), position = position_dodge() ) + labs(x = "2017", y = "# of Leads") + theme(axis.line = element_line(color = "black", size = 3, linetype = "solid"), axis.text.x = element_text(face = "bold", color = "black", size = 14),
  axis.text.y = element_text(face = "bold", color = "black", size = 14)) + scale_y_continuous(name = "# Of Leads", breaks = seq(0,1000, 50)) + theme(panel.background = element_rect(colour = "black",
  fill = "white"), panel.grid.minor = element_line(color = "black", size = .5), panel.grid.major = element_line(color = "black", size = .5)) +
  scale_fill_manual(values = c("#DA4424", "#24A0DA", "#F0BC0B", "#43F749", "#4348F7", "#F74369","#D7B9F5"))

编辑:或者我应该创建一个新的csv,其中包含每个Decile中每个Decile的最终计数。这将是一个快速修复,我可以很容易地从SQL服务器中提取数字。我只是希望这样做而不必创建新文件。

2 个答案:

答案 0 :(得分:1)

不确定这与真实数据有什么关系,但是你可以探索的东西在这些情况下看起来很好用的是使用 library(ggridges) library(dplyr) library(ggplot2) set.seed(99) MCD <- tibble( Name = sample(c("Joe", "Jay", "Susan", "Nancy", "Mark"), 1000, T), Decile = sample(1:10,1000, T) , Division = sample(c('San Diego', 'New York', 'Dallas', 'Seattle', 'LA'), 1000, T), MonthNo = sample(1:11, 1000 , T)) MCD$MonthNo <- factor(MCD$MonthNo, levels = c(1:11), labels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November")) MCD %>% group_by(Decile, MonthNo) %>% summarize(Count = n()) %>% ungroup() %>% mutate(Decile = factor(Decile)) %>% ggplot(aes(x = MonthNo, y = Decile, height = Count, group = Decile, fill = Decile)) + geom_density_ridges(alpha = 0.7, show.legend = F, stat = "identity", scale = 1) 包的山脊图。示例如下:

</div>

答案 1 :(得分:0)

这应该是"geom_line version of geom_bar"

df <- data.frame(x=c(1,1,1, 3,3, 10,10,10,10))
ggplot(df, aes(x=x)) + geom_line(stat="count")

更新: 以下是基于TBT8答案的数据集的版本

library(dplyr)
library(ggplot2)

set.seed(99)
#create dummy dataset 
MCD <- tibble(
Name = sample(c("Joe", "Jay", "Susan", "Nancy", "Mark"), 1000, T),
Decile = sample(1:10,1000, T) ,
Division = sample(c('San Diego', 'New York', 'Dallas', 'Seattle', 'LA'), 1000, T),
MonthNo = sample(1:11, 1000 , T))

MCD$MonthNo <- factor(MCD$MonthNo, levels = c(1:11), 
labels = c("January", "February", 
"March", "April", "May", 
"June", "July", "August", 
"September", "October", "November"))

#create a numeric months vector
MonthNum <- 1:11
names(MonthNum) <- levels(MCD$MonthNo)

#create a factor to be used for facets
MCD$DecileF <- factor(
  MCD$Decile, 
  levels=as.character(sort(unique(MCD$Decile),
                           decreasing = TRUE)))

ggplot(MCD, aes(x = MonthNum[MonthNo], col=DecileF)) + 
  geom_path(stat = "count") +
  #geom_point(stat = "count") + 
  facet_grid(DecileF~.) + 
  scale_x_discrete(name ="Month", limits=names(MonthNum))