ggplot更改x轴标签而不更改基础数据

时间:2017-08-18 14:38:36

标签: r ggplot2 axis-labels

我正在使用x轴上的facet和离散数据进行绘图。如何在不更改基础数据的情况下更改x轴标签?即而不是标有URL url = new URL("YOUR ULR"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.setDoOutput(true); conn.setConnectTimeout(6000); HashMap<String, String> params = new HashMap<>(); params.put("PARAMS", dataToSend); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); boolean first = true; StringBuilder result = new StringBuilder(); for (Map.Entry<String, String> entry : params.entrySet()) { if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } writer.write(result.toString()); writer.flush(); writer.close(); os.close(); StringBuilder serverResponse = new StringBuilder(); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = br.readLine()) != null) { serverResponse.append(inputLine); } response = serverResponse.toString(); 的标签,他们应该阅读"a","b","c","d"

"Lab 1","Lab 2","Lab 3","Lab 4"

1 个答案:

答案 0 :(得分:3)

您可以在labels中使用scale_x_discrete参数的命名字符向量。

scale_x_discrete(labels = c("a" = "Lab 1", "b" =  "Lab 2", "c" = "Lab 3", "d" = "Lab 4") )

或者,您可以同时提供breakslabels

scale_x_discrete(breaks = c("a", "b", "c", "d"), 
                 labels = c("Lab 1", "Lab 2", "Lab 3", "Lab 4") )