Manufacturer <- c("WARNER","TONYS","TOMBSTONE", "SNYDER", "SHULTZ", "QUAKER", "PRIVATE_LABEL", "POST_FOOD", "P&G", "MKSL", "KING", "KELLOGG", "GENERAL_MI", "FRITO_LAY", "COLGATE", "CHATTEM")
Max <- c(4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4)
Min <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
Price_elasticity <- c(1.343, 3.741, 3.612, 0.7562, 0.6249, 3.393, 0.7283, 1.117, 0.3996, 2.414, 3.024, 3.031, 1.470, 2.416, 0.2536, 1.129)
MANUF_PE <- data.frame(Manufacturer, Min, Max, Price_elasticity)

अगर मैं ऐसा करता हूं तो मुझे मिलता है

  Manufacturer     Min     Max     
1   WARNER          0       4      1.3430
2   TONYS           0       4      3.7410
3   TOMBSTONE       0       4      3.6120
4   SNYDER          0       4      0.7562
5   SHULTZ          0       4      0.6249
6   QUAKER          0       4      3.3930
7   PRIVATE_LABEL   0       4      0.7283
8   POST_FOOD       0       4      1.1170
9   P&G             0       4      0.3996
10  MKSL            0       4      2.4140 
11  KING            0       4      3.0240
12  KELLOGG         0       4      3.0310
13  GENERAL_MI      0       4      1.4700
14  FRITO_LAY       0       4      2.4160
15  COLGATE         0       4      0.2536
16  CHATTEM         0       4      1.1290

लेकिन मैं उम्मीद कर रहा था कि मैं इसे इस तरह दिखने के लिए प्राप्त कर सकता हूं:

this is how I want it to look

तो मैं इसे इस रडार ग्राफ के लिए उपयोग कर सकता हूं:

library(ggplot2)



radarchart( manu_pe  , axistype=1 , 

            #custom polygon
            pcol=rgb(0.8,0.2,0.5,0.9) , pfcol=rgb(0.8,0.2,0.5,0.4) , plwd=4 , plty=1, , 

            #custom the grid
            cglcol="grey", cglty=1, axislabcol="grey", caxislabels=seq(0,4,1), cglwd=0.2,

            #custom labels
            vlcex=0.8 
)

मैंने डेटाफ़्रेम फ़्लिप करके इसे करने की कोशिश की, लेकिन यह काम नहीं कर रहा था... कृपया मदद करें धन्यवाद!

1
new_R 23 फरवरी 2020, 21:17

3 जवाब

जहां तक ​​​​मांग किए गए डेटा परिवर्तन का संबंध है, एक साधारण आर आधार समाधान t का उपयोग करके स्तंभों को पंक्तियों में स्थानांतरित करना है और इसके विपरीत:

t(MANUF_PE)
                 [,1]     [,2]     [,3]        [,4]     [,5]     [,6]     [,7]            [,8]       
Manufacturer     "WARNER" "TONYS"  "TOMBSTONE" "SNYDER" "SHULTZ" "QUAKER" "PRIVATE_LABEL" "POST_FOOD"
Min              "0"      "0"      "0"         "0"      "0"      "0"      "0"             "0"        
Max              "4"      "4"      "4"         "4"      "4"      "4"      "4"             "4"        
Price_elasticity "1.3430" "3.7410" "3.6120"    "0.7562" "0.6249" "3.3930" "0.7283"        "1.1170"   
                 [,9]     [,10]    [,11]    [,12]     [,13]        [,14]       [,15]     [,16]    
Manufacturer     "P&G"    "MKSL"   "KING"   "KELLOGG" "GENERAL_MI" "FRITO_LAY" "COLGATE" "CHATTEM"
Min              "0"      "0"      "0"      "0"       "0"          "0"         "0"       "0"      
Max              "4"      "4"      "4"      "4"       "4"          "4"         "4"       "4"      
Price_elasticity "0.3996" "2.4140" "3.0240" "3.0310"  "1.4700"     "2.4160"    "0.2536"  "1.1290"
0
Chris Ruehlemann 23 फरवरी 2020, 21:37

हम इसे लंबे प्रारूप में बदल सकते हैं और फिर names_from 'निर्माता' कॉलम के साथ 'चौड़ा' में परिवर्तित कर सकते हैं

library(dplyr)
library(tidyr)
MANUF_PE %>% 
    pivot_longer(cols = -Manufacturer) %>%
    pivot_wider(names_from = Manufacturer, values_from = value)
# A tibble: 3 x 17
#  name  WARNER TONYS TOMBSTONE SNYDER SHULTZ QUAKER PRIVATE_LABEL POST_FOOD `P&G`  MKSL  KING KELLOGG
#  <chr>  <dbl> <dbl>     <dbl>  <dbl>  <dbl>  <dbl>         <dbl>     <dbl> <dbl> <dbl> <dbl>   <dbl>
#1 Min     0     0         0     0      0       0            0          0    0      0     0       0   
#2 Max     4     4         4     4      4       4            4          4    4      4     4       4   
#3 Pric…   1.34  3.74      3.61  0.756  0.625   3.39         0.728      1.12 0.400  2.41  3.02    3.03
# … with 4 more variables: GENERAL_MI <dbl>, FRITO_LAY <dbl>, COLGATE <dbl>, CHATTEM <dbl>

अगर हमें radarchart की जरूरत है

library(fmsb)
library(tibble)
MANUF_PE %>% 
    pivot_longer(cols = -Manufacturer) %>%
    pivot_wider(names_from = Manufacturer, values_from = value) %>% 
    column_to_rownames("name") %>% 
    radarchart(axistype = 1,  pcol=rgb(0.8,0.2,0.5,0.9),
     pfcol=rgb(0.8,0.2,0.5,0.4), plwd=4 , plty=1,  cglcol="grey", 
     cglty=1, axislabcol="grey", caxislabels=seq(0,4,1), cglwd=0.2, 
     vlcex=0.8 )

enter image description here

यदि कॉलम एक निर्दिष्ट क्रम में होना चाहिए

MANUF_PE %>% 
    pivot_longer(cols = -Manufacturer) %>% 
    pivot_wider(names_from = Manufacturer, values_from = value) %>%
    arrange(factor(name, levels = c("Max", "Min", "Price_elasticity"))) %>%
    column_to_rownames("name") %>% 
    radarchart(axistype = 1,  pcol=rgb(0.8,0.2,0.5,0.9) ,
      pfcol=rgb(0.8,0.2,0.5,0.4) , plwd=4 , plty=1,  cglcol="grey", cglty=1,
       axislabcol="grey", caxislabels=seq(0,4,1), cglwd=0.2, vlcex=0.8 )
3
akrun 23 फरवरी 2020, 21:27

आप data.table::transpose() का उपयोग कर सकते हैं। यदि इनपुट है तो आउटपुट एक नियमित डेटा फ्रेम होगा:

data.table::transpose(MANUF_PE,keep.names = "name",make.names = "Manufacturer")
#>               name WARNER TONYS TOMBSTONE SNYDER SHULTZ QUAKER PRIVATE_LABEL
#> 1              Min  0.000 0.000     0.000 0.0000 0.0000  0.000        0.0000
#> 2              Max  4.000 4.000     4.000 4.0000 4.0000  4.000        4.0000
#> 3 Price_elasticity  1.343 3.741     3.612 0.7562 0.6249  3.393        0.7283
#>   POST_FOOD    P&G  MKSL  KING KELLOGG GENERAL_MI FRITO_LAY COLGATE CHATTEM
#> 1     0.000 0.0000 0.000 0.000   0.000       0.00     0.000  0.0000   0.000
#> 2     4.000 4.0000 4.000 4.000   4.000       4.00     4.000  4.0000   4.000
#> 3     1.117 0.3996 2.414 3.024   3.031       1.47     2.416  0.2536   1.129
1
Moody_Mudskipper 24 फरवरी 2020, 03:14