在ggplotly散点图上添加辅助x和y轴

时间:2019-05-29 12:10:51

标签: r ggplot2 ggplotly

我有此数据:

structure(list(ID = structure(c(3L, 4L, 4L, 4L, 4L, 4L, 1L, 5L, 
2L, 2L), .Label = c("015753e0-8574-4b13-88d7-627292d52272", "2721a25c-cc0e-43d3-abd2-b29a0da8b9d0", 
"60a7614a-63bb-4bb9-91ac-520549853c19", "86735a77-b822-4320-b16e-1ff2b5535f5d", 
"8ff4d254-291b-47d4-970a-82f29f8a51fb"), class = "factor"), Impact = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 3L, 2L, 3L, 3L), .Label = c("Extreme impact", 
"Moderate impact", "Strong impact"), class = "factor"), Size_bribe = structure(c(1L, 
3L, 3L, 3L, 3L, 3L, 2L, 4L, 1L, 1L), .Label = c("10,000 Afs", 
"100 Afs", "100,000 Afs", "No payment made"), class = "factor"), 
    Abuse_type = structure(c(2L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 
    2L, 2L), .Label = c("Delay of service", "Payment of money", 
    "Threat of incarceration"), class = "factor"), service_worst_problem = structure(c(3L, 
    1L, 1L, 1L, 1L, 1L, 2L, 4L, 3L, 3L), .Label = c("Obtaining a judgement", 
    "Other", "Reporting a offence/crime to the police", "Vehicle registration"
    ), class = "factor"), Gender = structure(c(2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 1L, 2L, 2L), .Label = c("Female", "Male"), class = "factor"), 
    Age = structure(c(3L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L), .Label = c("26 - 35", 
    "36 - 45", "46 - 55"), class = "factor"), Education = structure(c(4L, 
    2L, 2L, 2L, 2L, 2L, 1L, 3L, 1L, 1L), .Label = c("I do not have any formal education", 
    "Primary education", "University or post-graduate education", 
    "Vocational training after Secondary / High School"), class = "factor"), 
    Madrassa = structure(c(1L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 
    1L), .Label = c("No", "Yes, on a regular basis", "Yes, some"
    ), class = "factor"), Monthly_income = structure(c(2L, 1L, 
    1L, 1L, 1L, 1L, 4L, 2L, 3L, 3L), .Label = c("20,001 - 25,000 AFN", 
    "6001 - 8000 AFN", "8001 - 10,000 AFN", "Less than 2,000 AFN"
    ), class = "factor"), local_influence = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "No", class = "factor"), 
    basis_local_influence = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 1L, 1L), .Label = c("", "None of the above"), class = "factor"), 
    current_occupation = structure(c(1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("Other", "Paid worker in any sector"
    ), class = "factor"), ethinic_group = structure(c(3L, 2L, 
    2L, 2L, 2L, 2L, 1L, 3L, 3L, 3L), .Label = c("Aimaq", "Pashtun", 
    "Tajik"), class = "factor"), location = structure(c(1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L), .Label = c("Herat PD1 - Dawlat Khana", 
    "Herat PD9 - Baghcha-e-Mastufi"), class = "factor"), City = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Herat", class = "factor"), 
    NarrID = c(173L, 174L, 174L, 174L, 174L, 174L, 175L, 177L, 
    178L, 178L), Stone.Num = c(1L, 1L, 2L, 3L, 4L, 5L, 4L, 1L, 
    1L, 2L), NarrID_Stone = c(173.01, 174.01, 174.02, 174.03, 
    174.04, 174.05, 175.04, 177.01, 178.01, 178.02), Stone.Name.L1 = structure(c(3L, 
    3L, 4L, 1L, 5L, 2L, 5L, 3L, 3L, 4L), .Label = c("District Officials", 
    "Malik / Wakil-e-Guzar", "Police", "Provincial Officials", 
    "Religious Leaders"), class = "factor"), Canvas01.AdjXRightValue = c(0.7299, 
    0.8994, 0.8615, 0.2399, 0.9103, NA, 0.8757, 0.0916, 0.1725, 
    0.351), Canvas01.AdjYTopValue = c(0.2903, 0.8374, 0.6583, 
    0.8183, 0.2167, NA, 0.8374, 0.7678, 0.705, 0.7239), Canvas01.Quadrant = structure(c(3L, 
    4L, 4L, 2L, 3L, 1L, 4L, 2L, 2L, 2L), .Label = c("", "2.  Upper Left", 
    "3.  Lower Right", "4.  Upper Right"), class = "factor"), 
    Canvas02.AdjXRightValue = c(0.2113, 0.8892, 0.8102, 0.1629, 
    0.8911, NA, 0.9206, 0.7614, 0.4365, 0.143), Canvas02.AdjYTopValue = c(0.2375, 
    0.8305, 0.7514, 0.8385, 0.1948, NA, 0.2521, 0.8824, 0.5588, 
    0.7078), Canvas02.Quadrant = structure(c(2L, 5L, 5L, 3L, 
    4L, 1L, 4L, 5L, 3L, 3L), .Label = c("", "1. Lower Left", 
    "2.  Upper Left", "3.  Lower Right", "4.  Upper Right"), class = "factor"), 
    Canvas03.AdjXRightValue = c(0.2945, 0.8503, 0.7177, 0.2971, 
    0.9026, NA, 0.1442, 0.6663, 0.1856, 0.6509), Canvas03.AdjYTopValue = c(0.2849, 
    0.7296, 0.7228, 0.7445, 0.1675, NA, 0.836, 0.8469, 0.7808, 
    0.8319), Canvas03.Quadrant = structure(c(2L, 5L, 5L, 3L, 
    4L, 1L, 3L, 5L, 3L, 5L), .Label = c("", "1. Lower Left", 
    "2.  Upper Left", "3.  Lower Right", "4.  Upper Right"), class = "factor"), 
    Canvas04.AdjXRightValue = c(0.2804, 0.9165, 0.8147, 0.7183, 
    0.1924, 0.6477, 0.6946, 0.0608, 0.1211, 0.3215), Canvas04.AdjYTopValue = c(0.2808, 
    0.8114, 0.7282, 0.7064, 0.2767, 0.7391, 0.746, 0.3553, 0.615, 
    0.6246), Canvas04.Quadrant = structure(c(1L, 3L, 3L, 3L, 
    1L, 3L, 3L, 1L, 2L, 2L), .Label = c("1. Lower Left", "2.  Upper Left", 
    "4.  Upper Right"), class = "factor"), Canvas05.AdjXRightValue = c(0.2817, 
    0.2078, 0.2065, 0.1205, 0.2958, 0.1166, 0.1115, 0.1532, 0.2642, 
    0.143), Canvas05.AdjYTopValue = c(0.2535, 0.2167, 0.3122, 
    0.1455, 0.2399, 0.3094, 0.8169, 0.2789, 0.2883, 0.2453), 
    Canvas05.Quadrant = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    1L, 1L, 1L), .Label = c("1. Lower Left", "2.  Upper Left"
    ), class = "factor")), row.names = c(NA, 10L), class = "data.frame")

,并且已经使用ggplotly()创建了此散点图:

ggplotly(
          ggplot(df_f, aes(x=Canvas01.AdjXRightValue, y=Canvas01.AdjYTopValue, colour=Gender)) + 
          geom_point(aes(text=map(paste('<b>Canvas:</b>',"1 size and frequency", 
                                        '<br>', '<b>Stone:</b>', Stone.Name.L1, 
                                        '<br>', '<b>Quadrant:</b>', Canvas01.Quadrant,
                                        '<br>', '<b>About the Respondents Experience</b>'," ",
                                        '<br>', '<b>Impact:</b>', Impact,
                                        '<br>', '<b>Size of bribe:</b>', Size_bribe,
                                        '<br>', '<b>Type of abuse:</b>', Abuse_type,
                                        '<br>', '<b>Service with worst problem:</b>', service_worst_problem,
                                        '<br>', '<b>About the Respondent</b>'," ",
                                        '<br>', '<b>Gender:</b>', Gender,
                                        '<br>', '<b>Age:</b>', Age,
                                        '<br>', '<b>Education:</b>', Education,
                                        '<br>', '<b>Madrassa:</b>', Madrassa,
                                        '<br>', '<b>Monthly income:</b>', Monthly_income,
                                        '<br>', '<b>Local influence?:</b>', local_influence,
                                        '<br>', '<b>Basis of local influence:</b>', basis_local_influence,
                                        '<br>', '<b>Current occupation:</b>', current_occupation,
                                        '<br>', '<b>Ethnic group:</b>', ethinic_group,
                                        '<br>', '<b>Location:</b>', location,
                                        '<br>', '<b>Default text</b>'," "), HTML)))+
          xlab("Asked for small bribes or abused power minimally") +
          ylab("Asked for bribes or abused power infrequently") +
          ggtitle("1 size and frequency") + geom_hline(aes(yintercept=0.5),colour="black") +
            theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                  panel.background = element_blank())+theme(legend.title = element_blank())+
          geom_vline(aes(xintercept=0.5), colour="black"))

一切正常,看起来不错,只是我需要在图表的4个侧面添加标题,现在我在底部和左侧都有标题,但想在顶部和右侧添加另一个标题。 我该怎么办?

在所有4个面上带有标题的图表示例: Image with titles on all 4 sides

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,因为我无法使其与plotly::ggplotly一起使用。我希望它能帮助您进一步

在对ggplot的调用中,您可以在sex.axisscale_x_continuous()函数中使用scale_y_continuous()参数,如以下代码所示:

ggplot(df_f, aes(x = Canvas01.AdjXRightValue, y = Canvas01.AdjYTopValue, colour = Gender)) + 
  geom_point() +
  xlab("Asked for small bribes or abused power minimally") +
  ylab("Asked for bribes or abused power infrequently") +
  ggtitle("1 size and frequency") + 
  geom_hline(aes(yintercept=0.5),colour="black") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank())+theme(legend.title = element_blank())+
  geom_vline(aes(xintercept=0.5), colour="black") + 
  scale_x_continuous(sec.axis = dup_axis(name = "Asked for large bribes\nor abused power significantly")) +
  scale_y_continuous(sec.axis = dup_axis(name = "Asked for bribes or abused\n their power frequently"))

产生以下情节:

enter image description here