+----+------+-------+
| Id | Time | Value |
+----+------+-------+
| 1 | A | 1 |
| 2 | B | 10 |
| 2 | C | 3 |
| 2 | D | 6 |
| 3 | E | 2 |
| 4 | F | 9 |
| 4 | G | 4 |
| 4 | H | 17 |
+----+------+-------+
I have data like above (simplified). I have drawn this using a scatter plot using plot-ly with Id as the X axis and value as the Y axis. Now based on the time column, I want the most recent entry for the Id field to have a specific shape/symbol. How can I do this using plot-ly and r?
I would add a column to flag the last entry, then use that column as an aesthetic .
For example, in ggplot
:
df %>%
group_by(Id) %>%
mutate(
rowNo = 1:n(),
isLast = factor(ifelse(rowNo == n(), 1, 0))) %>%
ggplot(aes(x = Id, y = Value, shape = isLast)) + geom_point()
And similarly for plot_ly
.
df %>%
group_by(Id) %>%
mutate(
rowNo = 1:n(),
isLast = factor(ifelse(rowNo == n(), 1, 0))) %>%
plot_ly(x = ~Id, y = ~Value, symbol = ~isLast)
df <- read.table(text =
"Id Time Value
1 A 1
2 B 10
2 C 3
2 D 6
3 E 2
4 F 9
4 G 4
4 H 17", header = T)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments