如何使用ggplot创建具有两个因素的构面?

时间:2018-09-09 08:32:07

标签: r ggplot2

我正在尝试使用包含来自先前模型的一些数据的构面进行绘图。举一个简单的例子:

t=1:10;
x1=t^2;
x2=sqrt(t);
y1=sin(t);
y2=cos(t);

我如何在2x2网格中绘制此数据,即行是一个因素(级别xy,用不同的颜色绘制)而列是另一个因素(级别12(以不同的线型绘制)?

注意:t是所有子图的X轴的公共变量。

1 个答案:

答案 0 :(得分:1)

如果可以先将数据放入tidy form,则

ggplot会更有用。 WiFiEspClient espClient; PubSubClient client(espClient); void callback(char* topic, byte* payload, unsigned int length) //print any message received for subscribed topic { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); } Serial.println(); } void WiFiConnect() { if (WiFi.status() == WL_NO_SHIELD) // check for the presence of the shield { Serial.println("WiFi shield not present"); while (true);// don't continue } while ( status != WL_CONNECTED) // attempt to connect to WiFi network { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network } Serial.println("You're connected to the network"); // you're connected now client.setServer(server, 1883); //connect to MQTT server client.setCallback(callback); } void reconnect(){ while (!client.connected()) // Loop until we're reconnected { Serial.print("Attempting MQTT connection..."); if (loadDataFromSensors()) { } if (client.connect("ESP8266Client")) // Attempt to connect, just a name to identify the client { Serial.println("connected"); client.subscribe("/Thingworx/PRM9_iot_pub/movespool"); client.publish("/Thingworx/PRM9_iot/SpoolPosition", spoolposString); client.publish("/Thingworx/PRM9_iot/Teplota", tempString); client.publish("/Thingworx/PRM9_iot/Vlhkost", humidString); client.publish("/Thingworx/PRM9_iot/Tlak", presString); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 2 seconds"); delay(2000); } } } 是您的数据,df是整洁的数据,其中的序列在一个列中标识,可以在df_tidy中映射-在这种情况下为构面。

ggplot

enter image description here