我想使用geo_bar
与facet,获得百分比而不是绝对计数,但百分比应相对于每个方面,而不是相对于总计数。
已经讨论了很多(example),建议使用geom_bar(aes(y = (..count..)/sum(..count..)))
。这不适用于facets(即将给出总计数)。已经提出了更好的解决方案,
使用stat_count(mapping = aes(x=x_val, y=..prop..))
代替。
如果x
数字,这似乎有效,但如果x
是字符则不行:所有条都是100%!为什么?难道我做错了什么?谢谢!
library(tidyverse)
df <- data_frame(val_num = c(rep(1, 60), rep(2, 40), rep(1, 30), rep(2, 70)),
val_cat = ifelse(val_num==1, "cat", "mouse"),
group=rep(c("A", "B"), each=100))
#works with numeric
ggplot(df) + stat_count(mapping = aes(x=val_num, y=..prop..)) + facet_grid(group~.)
# does not work?
ggplot(df) + stat_count(mapping = aes(x=val_cat, y=..prop..)) + facet_grid(group~.)
答案 0 :(得分:4)
添加namespace StackOverflow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DataGridRow lastRowClicked;
public MainWindow()
{
InitializeComponent();
}
private void OnRowLeftClicked(object sender, RoutedEventArgs e)
{
var clickedRow = (DataGridRow)sender;
//If a Todo has been clicked before for this selected user, we need to decide whether a new Todo has been selected.
if (lastRowClicked != null)
{
//If the Todo that was clicked last time is the same as the one that was just clicked, toggle its visibility --- this is where problem lies
if (lastRowClicked == clickedRow)
{
if (clickedRow.DetailsVisibility == Visibility.Collapsed)
clickedRow.DetailsVisibility = Visibility.Visible;
else
clickedRow.DetailsVisibility = Visibility.Collapsed;
}
//If this Todo was not clicked last time, hide the last Todo and show this current one.
else
{
lastRowClicked.DetailsVisibility = Visibility.Collapsed;
clickedRow.DetailsVisibility = Visibility.Visible;
}
}
//If this is the first Todo that was clicked for this user, we can show it without collapsing another row.
else
{
clickedRow.DetailsVisibility = Visibility.Visible;
}
//in any case, save the currently clicked row for next time
lastRowClicked = clickedRow;
}
}
}
告诉ggplot按group=group
计算比例,而不是默认值,这对于group
的每个级别都是单独的。
val_cat
当x变量是连续的时,默认情况下ggplot(df) +
stat_count(aes(x=val_cat, y=..prop.., group=group)) +
facet_grid(group~.)
似乎会计算构面中所有数据的百分比。但是,当x变量是分类时,stat_count
在每个x级别内分别计算百分比。看看以下示例会发生什么:
添加stat_count
作为组审美会导致在每个x级别内计算百分比,而不是在方面中的所有值。
val_num
将ggplot(df) +
stat_count(aes(x=val_num, y=..prop.., group=val_num)) +
facet_grid(group~.)
转换为因子同样会导致在每个x级别内计算百分比,而不是在构面中的所有值。
val_num