Pick color or symbol of marker in Plotly r on a group by condition

Viswanath Ramanan
+----+------+-------+
| 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?

Maurits Evers

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()

enter image description here

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)

Sample data

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.

edited at
0

Comments

0 comments
Login to comment

Related

R Plotly - Assign the color of a marker by its size

Issue changing marker color in R plotly scatter plot

R plotly marker/line color by value of other variable

Pick the maximum n values based on a condition in R for each group

Mapbox marker symbol color change

Plotly Dash | marker_symbol not applying correctly

Separate symbol and color in plotly legend

Python plotly_change marker's color

Marker Color based on String Value Plotly Python

ggbiplot - change the group color and marker

color and legend in plotly in R

R Plotly: Change legend symbol

How to add currency symbol to marker label plotly python

Plotly (R) - Pie chart: How to fixate the color assignment color per group?

Remove marker border/line in scatterplot plotly R

Observation number as marker in PCA using Plotly in R

Python plotly scatter chart, take marker color from dataframe

How to change color of text in plotly scatter and different marker?

How to format plotly legend when using marker color?

Plotly: How to set marker symbol shapes for multiple traces using plotly express?

Color mapping not working with plotly in R

Color surface by variable with plotly in R

Julia: How to change color of a group in plotly scatterplot

Plotly: Scatter plot with dropdown menu and color by group

Change color of markers of a trace in plotly with plotly proxy without changing the marker size

Subsetting by group condition in R

print group in R by condition

Group by with condition using r

Plotly | ValueError: Invalid element(s) received for the 'symbol' property of scatter.marker. Invalid elements include: [['circle']]