PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, April 23, 2022

[FIXED] How to add seperate titles to pie charts created through add_trace in plotly R

 April 23, 2022     pie-chart, plotly     No comments   

Issue

I am trying to create couple of pie diagrams using same data set but value variables will be different column names in the dataset. I need a help to print different title for each of the pie diagram printed.

Below is the code am using:Expectation is under each pie diagram i want to print a title as 'mpg','disp','hp' etc.. as these are the observations used as value variable.

        xmtcars<-tibble::rownames_to_column(mtcars,'carname')


           plot_ly(data = xmtcars)%>% add_trace(
           labels =  ~ carname,
           values =  ~ mpg,
           type = 'pie',
            textinfo = 'none',
            name = 'mpg',
            domain = list(x = c(0.0, .1667), y = c(0, 1)))%>% add_trace(
            labels =  ~ carname,
            values =  ~ cyl,
            type = 'pie',
            textinfo = 'none',
            name = 'cyl',
            domain = list(x = c(0.1667, .333), y = c(0, 1))
    )%>% add_trace(
            labels =  ~ carname,
            values =  ~ disp,
            type = 'pie',
            textinfo = 'none',
            name = 'disp',
            domain = list(x = c(0.333, .5), y = c(0, 1))
    )%>% add_trace(
            labels =  ~ carname,
            values =  ~ hp,
            type = 'pie',
            textinfo = 'none',
            name = 'hp',
            domain = list(x = c(.5, .667), y = c(0, 1))

    )%>% add_trace(
            labels =  ~ carname,
            values =  ~ drat,
            type = 'pie',
            textinfo = 'none',
            name = 'drat',
            domain = list(x = c(.667, .833), y = c(0, 1))

    )%>% add_trace(
            labels =  ~ carname,
            values =  ~ wt,
            type = 'pie',
            textinfo = 'none',
            name = 'wt',
            domain = list(x = c(0.833, 1), y = c(0, 1))

    ) %>%layout(
            title = "Analysis by cars",
            showlegend = T,
            xaxis = list(
                    showgrid = F,
                    zeroline = FALSE,
                    showticklabels = T
            ),
            yaxis = list(
                    showgrid = F,
                    zeroline = FALSE,
                    showticklabels = T
            )
    )

Appreciate some help.


Solution

You can use add_annotations and set the title of pies individually.

Just add this lines of code:

%>% add_annotations(x=seq(0.06,0.06 + 5*0.17,0.17),
                y=0.3,
                text = c("mpg", "cyl", "disp","hp","drat","wt"),
                xref = "paper",
                yref = "paper",
                xanchor = "left",
                showarrow = FALSE
                )

Here is the output: enter image description here



Answered By - vpz
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing