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

我有这个数据:

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 个面都有标题的图表示例:

最佳答案

这不是一个完整的答案,因为我无法让它与 plotly::ggplotly 一起工作。我希望它能帮助你更进一步

在对 ggplot 的调用中,您可以在 scale_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"))

产生以下情节:

https://stackoverflow.com/questions/56360330/

相关文章:

azure-active-directory - 企业应用程序中基于组角色的授权

google-chrome - 本地主机 Web 应用程序访问远程 API 时未设置 Cookie

sql-server - SQL Server Management Studio v18 的命令行

python - 无法编码响应 : datetime. datetime(2019, 5, 29,

reactjs - 我们如何在使用 react 最终形式时在组件中设置状态

git - 一个 git 子模块卡在错误的提交 ('submodule update' 不工作)

python - 是否可以在 Snakemake 中有一个可选的输出文件?

angular - 如何在构造函数中选择性地注入(inject) MAT_DIALOG_DATA

google-chrome-extension - Chrome 扩展程序 - 不在请求中发送 co

python - 消息 : Error: Polling for changes failed: N