diff --git a/dot.cabal b/dot.cabal
--- a/dot.cabal
+++ b/dot.cabal
@@ -1,5 +1,5 @@
 name:                dot
-version:             0.2.0
+version:             0.2.1
 synopsis:            Data types and encoding for graphviz dot files
 description:         Please see README.md
 homepage:            https://github.com/andrewthad/dot#readme
diff --git a/src/Dot/Text.hs b/src/Dot/Text.hs
--- a/src/Dot/Text.hs
+++ b/src/Dot/Text.hs
@@ -10,6 +10,7 @@
 import Data.Text.Lazy.Builder (Builder)
 import qualified Data.Text.Lazy.Builder as Builder
 import qualified Data.Text.Lazy as LText
+import qualified Data.Text as Text
 import Data.Monoid
 import Dot.Types
 
@@ -35,7 +36,11 @@
   <> "}"
 
 encodeId :: Id -> Builder
-encodeId (Id theId) = Builder.fromText theId
+encodeId (Id theId) = case Text.uncons theId of
+  Just (c,_) -> if not (c >= '0' && c <= '9') && Text.all (\c -> (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') theId
+    then Builder.fromText theId
+    else "\"" <> Builder.fromText (Text.replace "\"" "\\\"" theId) <> "\""
+  Nothing -> "\"\""
 
 encodeNodeId :: NodeId -> Builder
 encodeNodeId (NodeId theId mport) =
