我想在R中另一个顶部绘制一个条形图。 首先是所有元素的计数为0,然后是所有元素的计数,在它之上。
我在R:
中尝试了这段代码library(ggplot2)
var <- c(0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0)
ggplot(data.frame(var), aes(factor(var), fill=factor(var))) + geom_bar(stat="count", position="stack")
但它产生了这个情节:
这不是我想要的。 我想得到这样的东西(我用KolourPaint制作):
有关如何操作的任何建议吗?谢谢!
答案 0 :(得分:1)
问题是你已经为x审美提供了一个变量,is null limit 1
,但是从你说的那个,你实际上并不想要它。您可以在Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('displayName');
$table->string('email')->unique();
$table->string('role')->nullable();
$table->string('department')->nullable();
$table->string('location')->nullable();
$table->string('directDialIn')->nullable();
$table->string('mobileNumber')->nullable();
$table->string('managedByUsername')->nullable();
$table->string('managedByDisplayName')->nullable();
$table->timestamps();
$table->softDeletes();
});
中将一些虚拟变量用作x:单个数字或字母,甚至只是空白。
另请注意,count是QueryExecuted {#736 ▼
+sql: "select * from `users` where `username` = ? and `users`.`deleted_at` is null limit 1"
+bindings: array:1 [▼
0 => "jesseo"
]
+time: 6.25
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
QueryExecuted {#7458 ▼
+sql: "select * from `users` where `username` = ? and `users`.`deleted_at` is null limit 1"
+bindings: array:1 [▼
0 => "stevew"
]
+time: 0.62
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
QueryExecuted {#14214 ▼
+sql: "select `roles`.*, `model_has_roles`.`model_id` as `pivot_model_id`, `model_has_roles`.`role_id` as `pivot_role_id`, `model_has_roles`.`model_type` as `pivot_model_type` from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `model_has_roles`.`model_id` = ? and `model_has_roles`.`model_type` = ? ◀"
+bindings: array:2 [▼
0 => "jesseo"
1 => "App\User"
]
+time: 0.84
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
QueryExecuted {#21003 ▼
+sql: "select * from `users` where `users`.`username` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `users`.`deleted_at` is null"
+bindings: array:14 [▼
0 => 102
1 => "stevew"
2 => "Steve Williams"
3 => "Steve.Williams@newable.co.uk"
4 => "Web Manager, Digital"
5 => "Digital"
6 => "Head Office"
7 => "+44 (0)20 7940 1598 "
8 => ""
9 => "elouttit"
10 => "Edward Louttit"
11 => null
12 => null
13 => null
]
+time: 0.76
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
的默认统计信息,因此您无需明确提供factor(var)
。
aes
由reprex package(v0.2.0)创建于2018-05-08。
答案 1 :(得分:0)
快速而肮脏的解决方案是添加一个额外的变量以在x轴上使用。
var <- c(0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0)
var=as.data.frame(var)
var$var1=1
ggplot(data.frame(var), aes(factor(var1), fill=factor(var))) + geom_bar(stat="count", position="stack")