格式化ggplot2中的Geom_Bar

问题描述 投票:0回答:1

嗨,我在弄清楚如何正确格式化我要在ggplot2中生成的堆积条形图时遇到了一些麻烦。我尝试搜索以前的问题,但似乎都无法回答我遇到的问题。Geom_Bar using 1 + 2 as dummy variables在第一个所附图表中,我与图表中的期望值很接近,但是侧面的比例显示5个值与(“ 1”和“ 2”)的对比,这是框架中仅有的两个变量。本质上,我试图将填充比例固定为仅具有“ 1”和“ 2”值,并且如果可以将其编辑为“是”和“否”,请在下面附加代码:

    ggplot(AggSignedDummyVar, aes(fill=AggSignedDummyVar$`Signed by Drafting Club`, x = AggSignedDummyVar$`College Conference`, y = MLS_Draft_File$`Signed by Drafting Club`)) + 
  xlim('American Athletic Conference', 'Atlantic-10 Conference', 'Atlantic Coast Conference', 'Big East Conference', 'Big West Conference', 'Ivy League', 'Mid-American Conference', 'Pac-12 Conference', 'West Coast Conference') 

我还尝试使用('Yes'和'No')从上面重写代码,而不是上面代码中的伪变量。该部分似乎保留了发生的次数,但没有显示它们,而是在Y轴的下部(不应存在)附加“是”和“否”。 Geom_bar but without Dummy Variable。我在下面附加了代码:

    ggplot(MLS_Draft_File_Aggregated_Non_Numeric_, aes(fill=MLS_Draft_File_Aggregated_Non_Numeric_$`Signed by Drafting Club`, x = MLS_Draft_File_Aggregated_Non_Numeric_$`College Conference`, y = MLS_Draft_File_Aggregated_Non_Numeric_$`Signed by Drafting Club`)) + 
  xlim('American Athletic Conference', 'Atlantic-10 Conference', 'Atlantic Coast Conference', 'Big East Conference', 'Big West Conference', 'Ivy League', 'Mid-American Conference', 'Pac-12 Conference', 'West Coast Conference') 

希望我能正确解释,并在此先感谢您提供的任何帮助。

r ggplot2 geom-bar
1个回答
0
投票

如果提供ggplot::aes函数变量,ggplot会尝试猜测如何您要使用此数据。如果数据是数字,则即使只有两个不同的值,也会将其用作连续变量。如果提供它,则离散变量ggplot会相应地使用它。

请考虑以下两个情节:

library(ggplot2)
ggplot(mtcars, aes(x=mpg, y=hp, fill=cyl)) + geom_bar(stat="identity")

continuous variable for fill

fill的变量是数字-> ggplot将其视为连续的

但是这里:

ggplot(mtcars, aes(x=mpg, y=hp, fill=factor(cyl))) + geom_bar(stat="identity")

factor for fill

我们将cyl重新铸造为因数,然后传递给aes(我们也可以使用字符,但是factor具有可以指定级别顺序的优点。此顺序将由ggplot使用)

© www.soinside.com 2019 - 2024. All rights reserved.