r - 如何使用数据表的两列在ggplot2中定义x轴上的范围
问题描述
我有以下数据表
name rating min_date max_date weight
1 abc123 39 2018-04-29 2018-04-29 2000
2 abc123 80 2018-04-16 2018-07-31 3131
使用它,我想使用 ggplot 库为每一行绘制一条线,在 x 轴上保持最小日期到最大日期,在 y 轴上进行评级,并且线的颜色会根据重量而变化。
解决方案
我更改了数据,因为第一行中的数据相同min_date
,并且max_date
只有两个观察结果。
data <- read.table(text = "name rating min_date max_date weight
1 abc123 39 2018-04-29 2018-06-29 2000
2 abc123 80 2018-04-16 2018-07-31 3131
3 abc123 56 2018-04-15 2018-05-30 1831", header = T)
一种选择是weight
在绘图中作为连续变量:
library(ggplot2)
ggplot(data, aes(x = min_date, xend = max_date,
y = rating, yend = rating,
col = weight)) +
geom_segment()
第二种选择是将 is 作为一个因素:
ggplot(data, aes(x = min_date, xend = max_date,
y = rating, yend = rating,
col = as.factor(weight))) +
geom_segment()
推荐阅读
- ios - 在函数中编辑变量,然后在另一个函数中调用编辑的变量
- java - recyclerview 中更改行的问题
- docker - 滚动更新共享文件夹/文件 Docker
- c# - 检查字符串是否包含文本 C#
- python - 粘贴东西时python终端什么时候自动按回车
- oracle - 特定小时内的最大计数数
- c++ - 如何创建一个可以在任何具有字符串键的 std::map 上操作的模板化函数?
- pycharm - 类型提示不适用于 PyCharm 中的全局变量
- c++ - 在 unique_ptr<> 中使用自定义删除器 (curl_formfree())
- laravel - 如何使用 laravel 将多张图片上传到数据库中?